In Selenium-Rc/WebDriver automation xpath is the one of the way to locate elements on webpage.XPath uses path expressions to select nodes/element in an XML/ HTML document.
How to verify in firebug?
- Open firebug in firefox/Chrome.
- Go to console.
- type : $x("<xpath>").
- Hit Enter.
- 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:
How to write Xpath?
Examples
Facebook signup form
1. Select first name
//input[@id='firstname] OR //input[@name=firstname]
In this case no need of xpath locator because we can use By.id, By.name,By.cssSelector.By.id("firstname") | By.name("firstname") | By.cssSelector("[id=firstname]")
2. Selecting first name input using First Name label
//label[text()='First Name:']/parent::td/following-sibling::td/div/input
or
//label[text()='First Name:']/../../td/div/input
In above example,parent | This keyword helps you to back track to parent node from child node. |
following-sibling | This keyword helps you to go following-sibling. |
preceding-sibling | This keyword helps you to go preceding-sibling. |
.. | This also helps you to go parent node from child node |
3. Using contains, starts-with
Select Last Name using contains | //label[contains(text(),'Last')]/../../td/div/input |
Select Last Name using starts-with | //label[starts-with(text(),'Last')]/../../td/div/input |
4. Index/position based
5. Some more expressions
If you have any doubts in xpath..Post your html code snippet here I would try to give you solution.
Happy Coding..!
Regards,
Santosh
Hey Santosh, I have some problems running my TC with Internet Explorer, cannot find elements with xpath! Can you tell me how to handle this??
ReplyDeleteHave you tried with css selector?
DeleteIs xpath locator not working completely or some are not working?
Hi please tell me the xpath for the following code:
ReplyDeletediv id="drilldown-orgunitsSection" class="infobox fluid"
div class="header"
span class="pointer_hand" onclick="WebSct.toggleViewSection(this,'views_divdrilldown-orgunitsSection'); return false;"
div style="width: auto;margin-top:3px;"
div class="toolbar" style="float:right;margin-top*: -18px!important;"
dl
dt Actions:dt dd
ul
li
span
a id="fullexport" class="slick-box" data-id="export-drilldown-orgunits" href="" Export/a
/span