SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Results 1 to 4 of 4
  1. #1
    New Member
    Join Date
    Jan 2016
    Posts
    2
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Generic way to get elements

    Hi All,

    Using selenium, is there any way where we can find the element properties using the associated labels.

    We have below HTML Code.
    <div>
    <label for="Email" class="hidden-label">
    Enter your email</label>
    <input type="email" autofocus="" spellcheck="false" value="" placeholder="Enter your email" name="Email" id="Email">
    <input type="password" class="hidden" spellcheck="false" id="Passwd-hidden">
    </div>

    When we use "Enter your Email" as the text to get the elements like , Name, Xpath ,Id, CSS Selector which asociated with Label "Enter Your Email".

    Any reference to the question asked will be really helpful.

    Thanks In Advance.

    Ranjith

  2. #2
    SQA Knight
    Join Date
    May 2006
    Location
    Playa Del Rey, California, United States
    Posts
    2,595
    Post Thanks / Like
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Total Downloaded
    0
    Quote Originally Posted by ranjith302 View Post
    Hi All,

    Using selenium, is there any way where we can find the element properties using the associated labels.

    We have below HTML Code.
    <div>
    <label for="Email" class="hidden-label">
    Enter your email</label>
    <input type="email" autofocus="" spellcheck="false" value="" placeholder="Enter your email" name="Email" id="Email">
    <input type="password" class="hidden" spellcheck="false" id="Passwd-hidden">
    </div>

    When we use "Enter your Email" as the text to get the elements like , Name, Xpath ,Id, CSS Selector which asociated with Label "Enter Your Email".

    Any reference to the question asked will be really helpful.

    Thanks In Advance.

    Ranjith
    In your above example, By.ID and By.Name is the easiest way. You don't have to do anything special. (labels tend to have ID properties, so most likely the ID method will work)

    Let's say there wasn't an ID or unique name property. Generally, what I like to do is use CSS selectors because these are indexed by the browser and can be found quickly (ID is the quickest). The easiest way to add easy locators is just ask the developer, or if you can edit the source yourself, add extra css classes. Just agree with your developers on a prefixed name space and you can do things like this...
    Code:
       <input type="email" autofocus="" spellcheck="false" value="" placeholder="Enter your email" class="hidden automation-label-password" spellcheck="false" id="Passwd-hidden">
    
      // and you can easily find this element by
      var emailInput= driver.FindElementBy(By.CssSelector(".automation-label-password");
    But if for some political reason you can't do that. You can use XPath DOM traversal. This gets very ugly and is very slow in terms of element acquisition speed, it's not preferred.
    Code:
      var emailInput= driver.FindElementBy(By.XPath("//label[contains(.,''Enter your email")]/../input");
    Here I'm selecting the label that contains the desired text, then I'm using ".." to pop up to the parent element, then selecting the first input field. This method, I'll reiterate is not preferred. It's very ugly and very slow. Other automation engineers will laugh at you if you write that code.
    David Lai
    SDET / Consultant
    LinkedIn profile

  3. #3
    New Member
    Join Date
    Jan 2016
    Posts
    2
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0
    Thanks David for the response. I was able to enter the element using Xpath. I posted this question to find out are there any other ways?.
    Let me details out the intention behind this post.

    We often use different properties to find out an element. I was trying to make it simple for testers for identifying the elements.
    My thought process was , use the display text ()on the UI, then get all the properties of the element using the display text and use the same to perform click/enter text.

    Ex:
    1. Launch "Google.com"
    2. Enter the text in "Search". - Search is the label for the input.
    3. Click "Google Search" - Google search (Button Name).

  4. #4
    SQA Knight
    Join Date
    May 2006
    Location
    Playa Del Rey, California, United States
    Posts
    2,595
    Post Thanks / Like
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Total Downloaded
    0
    Quote Originally Posted by ranjith302 View Post
    Thanks David for the response. I was able to enter the element using Xpath. I posted this question to find out are there any other ways?.
    Let me details out the intention behind this post.

    We often use different properties to find out an element. I was trying to make it simple for testers for identifying the elements.
    My thought process was , use the display text ()on the UI, then get all the properties of the element using the display text and use the same to perform click/enter text.

    Ex:
    1. Launch "Google.com"
    2. Enter the text in "Search". - Search is the label for the input.
    3. Click "Google Search" - Google search (Button Name).
    You can wrap the xpath you're using into a function that generates them.
    Code:
    function(labelText) {
      var label = driver.findElementBy(By.XPath("//*[.='" + labelText + "']");
      var idTarget = label.getAttribute("for");
      return driver.findElementBy(By.ID(idTarget));
    }
    David Lai
    SDET / Consultant
    LinkedIn profile

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 11.54%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 06:57 PM.

Copyright BetaSoft Inc.