Friday, July 10, 2015

Locators or Idenfying web elements

Webex training notes:
7/10/15

  • We have 8 locators, using which selenium webdriver identifies the web element.
  • Using any one of the locators one shouldbe able to identify/locating  element in a web page.
  • <driverobject>.findElement(By.name("email_to[]")).click(); ---here name is one of the locators.
  • Right click on the object and select inspect by firebug. Especially html script will be seen, when you select the code it will highlight the related area in the web page.
  • If the attribute is common across multiple web elements, then we use firepath - xpath.
          For the xpath:
           select the web element, right click -> select inspect by firepath->show absolute xpath.

           driverobj.findElement(By.xpath("<>");

  • If absolute reference is not working we need to go with relative reference(using // in the xpath).
  • The brower that gets loaded using the script, using that we will not be able to inspect the element, because the script uses the default settings of browser. Additional settings that user does will not have impact on the script. To include your settings, we need to create profile using setproperty.
  • Xpath could be created as to make sure it matches related properties that we mention in it, like we do using regular expression.
          Ex: Creating xpath for 3 checkboxes thar are in an order in a web page.

          xpath = //<tag name>[@<attribute name>='<value>'] ....... Since we have picked name attribute here, the inspect element with this xpath will highlight all 3 checkboxes.

  • xpath = //<tag name>[@<attribute name>='<value>'] [1] --- this will locate the first element with the same attribute.
  • We can join two conditions using and
          xpath = //<tag name>[@<attribute name>='<value>'  and @value='1']

  • Use Higlight, adjacent to Xpath section in inspect by Firepath window, when you click Highlight it will show the web element that xpath is refering too. That way you can validate the xpath, whether its pointing out to the proper web element. Dont forget to enter xpath in the field.
  • Regular Expression in xpath:
           xpath = //<tag name>[starts-with(@<attribute name>,'<value>')]

           Following could be used:
           ends-with
           contains
  • Dropdown:
           We need to create object for class Select

           Select <object name> = new Select(<web Element>);

           Ex: Select dd1= new Select(driver.findElement(By.name("subject"));
           dd1.selectIndex(1);
          
          <object name>.selectByIndex("<value>");
          <object name>.selectByValue("<value>")
          <object name>.selectByVisibleText("<value>");

Lets suppose you wanted to select the last value of drop down, make use of index numders...index starts from 0. But you if you wanted to click the last but one drop down value, you need to know the total number of values in the drop down. To do that activity do the following.

 <object name>.getOptions().size()   ===  gives the total number of values in drop down.


  • xpath:
        //  --- use this if the element is grand child
xpath = //table[@class='fd']/td[@class='gfhy']
        / ---- use this if the element is direct child
xpath = //table[@class='fd']//a[@class='asd']

  • xpath:
Always use @ symbol for the atrributes

  • contains
  • text() --- see i have come across a scenario where I need to select a text that is a hyperlink. Here keep in mind text was not a attribute.
<table>
<tc>
--
<td class='sdf']
<a class ='dfs' href='#']Program</a>
--
--
</td>
--
--
</table>//td[@class='sdf']/a[@class='dfs' and text() = 'Program]



xpath = //table

 - How to send encoded value to Password field...?



- Source:
http://blargon7.com/selenium_docs/04_selenese_commands.html

To find list of commands that we can use in Selenium to locate an object. Its really good.


          

No comments:

Post a Comment