Find textbox by label.

8 posts, 0 answers
  1. Jason Evans
    Jason Evans avatar
    1 posts
    Member since:
    Jun 2012

    Posted 29 Mar 2011 Link to this post

    Hi there.

    In WatIn, you can do stuff like this:

    WebBrowser.Current.TextFields.First(Find.ByLabelText(labelText)).TypeText(value);
    

    Which finds a textbox by it's associated label and sets the text for that textbox. Is there a WebAii way of doing the above i.e. finding a textbox by the label associated to it?

    Cheers.
    Jas.
  2. Jason Evans
    Jason Evans avatar
    1 posts
    Member since:
    Jun 2012

    Posted 30 Mar 2011 Link to this post

    Hi there.

    OK, I think I worked it out. I use a HtmlFindExpression to do the job:

    HtmlFindExpression expression = new HtmlFindExpression("tagname=label", "textcontent=Username");
     
    var label = Find.ByExpression(expression);
     
    if (label != null)
    {
        if (label.ContainsAttribute("for"))
        {
            string textBoxName = label.GetAttribute("for").Value;
     
            Find.ById<HtmlInputText>(textBoxName).Text = "Got you!";
        }
    }


    In my tests, I have replaced the hardcoded

    'textcontent=Username'

    to

    'textcontent=' + paramLabelText

    This looks to be working fine for me.

    Cheers.
    Jas.
  3. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 31 Mar 2011 Link to this post

    Hi Jason,
        Find Expressions are definitely the way to go. Check out this page if you haven't done so already:
    http://www.artoftest.com/support/webaii/topicsindex.aspx?topic=byexpression

    You can rewrite this:
    HtmlFindExpression expression = new HtmlFindExpression("tagname=label", "textcontent=Username");
      
    var label = Find.ByExpression(expression);
    To look like this:
    HtmlControl label = Find.ByExpression<HtmlControl>("TextContent=Female", "tagname=label");
    But of course they both do the exact same thing.

    Let me know if you require any additional assistance!

    Best wishes,
    Stoich
    the Telerik team
  4. Jason Evans
    Jason Evans avatar
    1 posts
    Member since:
    Jun 2012

    Posted 01 Apr 2011 Link to this post

    Hi there.

    Many thanks for the code tip, I will give that a go.

    Cheers.
    Jas.
  5. Sanjay
    Sanjay avatar
    2 posts
    Member since:
    Jun 2014

    Posted 02 Jul 2014 in reply to Stoich Link to this post

    How to get the value of label from HtmlControl.
  6. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 07 Jul 2014 Link to this post

    Hello Sanjay,

    Do you have a snippet of HTML code as an example of the kind of text/value you want to get? Here's a simple example taken from this tutorial of how a Label is typically used:
    <form action="demo_form.asp">
      <label for="male">Male</label>
      <input type="radio" name="sex" id="male" value="male"><br>
      <label for="female">Female</label>
      <input type="radio" name="sex" id="female" value="female"><br>
      <input type="submit" value="Submit">
    </form>

    There are two labels on this page, one for the Male radio button and one for the Female radio button. Notice that both are simple HTML <label> elements with text content. Once you have an element you get the text value via the TextContent property like this:
    Element maleLabel = ActiveBrowser.Find.ByAttributes("for=male");
    string text = maleLabel.TextContent;

    I hope this helps.

    Regards,
    Cody
    Telerik
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
  7. Sanjay
    Sanjay avatar
    2 posts
    Member since:
    Jun 2014

    Posted 12 Aug 2014 Link to this post

    I Coddy I have HTML Something like


    <span class="x-panel-header-text" id="ext-gen308">rBook - Live Retail Market View</span>

    I would like to get the " rBook - Live Retail Market View ".
    How can i achieve that?

    Thanks
  8. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 14 Aug 2014 Link to this post

    Hi Sanjay,

    That value is what we call the "TextContent" of the element. This can be done either via non-coded step as either a Verify Text Content or Extract Text Content into Variable X. You should use our Elements Menu to create either one of those test steps.

    You could also get it via code if you prefer:
    HtmlSpan mylabel = ActiveBrowser.Find... [code here to find the right element]
    string text = mylabel.TextContent;


    Regards,
    Cody
    Telerik
     
    Quickly become an expert in Test Studio, check out our new training sessions!
    Test Studio Trainings
     
Back to Top