Thursday, 8 May 2014

How to identify web elements using XPATH in Selenium Webdriver



1) //html tag[@attribute_Name = 'attribute Value']

Ex:  //input [@id='text']



2) Any Html Tag

Ex: *[@id='text']



3) Duplicate element

//descendent :: input[@id='text']



4) Using more than two element

//input[@id='text' and name='username']



5) Element contains value

//html tag[contains(@attribute_name,'attribute_value')]
//input [contains(@id,'xt')



6) Element Starts with value

//input[starts-with(@id,'te')]



7)identify the last element

//input[@id='text'][1]]
//input[@id='text'][2]]
//input[@id='text'][last()]



8) Eliminate (Truncate) header and tailor spaces

we should use correct text including space while use element value. but its diffcult to find the spaces for element value using firebug. so we can use NORMALIZE-SPACE for  Eliminate (Truncate) header and tailor spaces.

//*[normalize-space(@value)="click me"]  


*************************************************************************


How to work with XPATH in selenium webdriver


Below symbol used to write the Xpath


1) "/" used to Search the element in root node.

EX: driver.findelement(By.xpath("/html/body/form/div[3]/div/div/ul/li[3]/span[@id='red']"))

Search element from root (html).


2) "//" used to Search the element in whole document. selection no matter where they are

  EX:  driver.findelement("//li[3]/span[@id='red']")

Seacrh element from third occurence li (li[3]) in whole document at any where. its not search on specific location.


3) "." used to Selects the current node

4) ".." used to Selects the parent of the current node

5) "@"  Selects the attribute

   Ex: <li id="lstproducts_ctrl5_prodlistitm" flag="AddToCart">
<a id="DUP012" title="Take Two" href="ProductDetails_US/Take-training.aspx">
<input id="nADD" class="cart" type="submit" " value="Green" name="lst$ctrl5$btnADD" alt=" Cart" title=" Cart">
</li>


driver.findelement("//li[3]/input[@value='Green']")

Here "value" is the attribute inside the input element




consider below example


<html>
<body>
<form>
<li id="lstproducts_ctrl5_prodlistitm" flag="AddToCart">
<a id="DUP011" title="Take Two" href="ProductDetails_US/Take-training.aspx">
<input id="nADD" class="cart" type="submit" " value="Blue" name="lst$ctrl5$btnADD" alt=" Cart" title=" Cart">
</li>

<li id="lstproducts_ctrl5_prodlistitm" flag="AddToCart">
<a id="DUP012" title="Take Two" href="ProductDetails_US/Take-training.aspx">
<input id="nADD" class="cart" type="submit" " value="Green" name="lst$ctrl5$btnADD" alt=" Cart" title=" Cart">
</li>


<li id="lstproducts_ctrl5_prodlistitm" flag="AddToCart">
<a id="DUP013" title="Take Two" href="ProductDetails_US/Take-training.aspx">
<input id="nADD" class="cart" type="submit" " value="Red" name="lst$ctrl5$btnADD" alt=" Cart" title=" Cart">
</li>

</form>
</body>
</html>




o/p  (Buttons)


Blue  Green  Red




1) How to click button ("Blue") using xpath

-> driver.findelement(by.xpath("/html/body/form/li[1]/input[@value='Blue']")).click
-> driver.findelement(By.xpath("//input[@value='Blue']").click


2) How to click second Button ("Green") using xpath

-> driver.findelement(by.xpath("/html/body/form/li[2]/input[@value='Green']")).click
-> driver.findelement(By.xpath("//input[@value='Green']").click


3) How to click randomly any button at run time

List(WebElement) CountButton=driver.findelements(By.Xpath("//input[@class='cart']")

Random rg=new Random(CountButton);
Integer RandomNumber1=rg.nextInt(rg);

driver.findelement(By.xpath("//li["+RandomNumber1+"]/input[@class='cart']")).click();



********************************************************************************************************************************


How to verify Xpath statement in firebug?


1) Open firebug in firefox/Chrome.
2) Go to console.
3) type : $x("<xpath>").
4) Hit Enter.
5) If it is correct path you can element in that console. If you mouse hover on that, the element will highlight in web page.


Example:

type : $x("//input[@value='Green']") in console



No comments:

Post a Comment