+5 votes
16.2k views
in Software Testing by (1.6k points)
retagged by
I want to know how can I Create a Xpath for Selenium Web Driver. want a Xpath of a element in HTML page.

Please elaborate with example.
closed

7 Answers

+4 votes
by Expert (5.9k points)
edited by
 
Best answer

First of all, I would say we should know about XPATH, yes the question comes to the mind What is XPATH ? Let's see the technical definition of XPath :

  • “XPATH is used to find information in an  XML and also used to find address element in any document or logical structure.”
  • When we talk about XPath in selenium, it means we are finding element address from a web page.there are several ways to finding an element address from a web page.

Some of are following:

 

  • By ID : ID is an attribute of any element in web page like there is an element say

<div id=”order”> here order is the id and the selenium syntax for finding element by ID would be :                           

    driver.findElementById("order");

  • By CSS : CSS is Cascade style sheet is used for formatting the documents written is markup languages like HTML. like here we can use CSS address in selenium and it looks like (#q38 > div.qa-q-view-main > form > div.qa-q-view-content > div)

can be used like :

driver.findElement(By.cssSelector("#q38 > div.qa-q-view-main > form > div.qa-q-view-content > div"))

  • By XPath : XPath is the path which locates an element in web page, It is already explained above in the starting of answer, It looks like

/HTML/body/div[2]/div[3]/div/div[3]/form/h1/span[2]

it can be used in selenium like :

driver.FindElement(By.XPath("/html/body/div[2]/div[3]"));

   These are the mainly used methods to finding  element address in selenium rather than these other methods are also available in selenium like ,By.linktext, by.Name , By.partiallink etc.

I hope this information would help you!

0
by
Add below addon to your firefox, you can get better combinations of xpath:

https://addons.mozilla.org/en-US/firefox/addon/element-locator-for-webdriv/?src=api
+3 votes
by (530 points)
edited by
The simplest way to find Xpath of any element in html is you can install view Xpath firefox plugin but in this you need to remove unnecessary marks.

Other way is you can install firefox Selenium IDE plug in, in which you can record and see the xpath.

These are two simplest way to get xpath.
0
by (1.6k points)
@raman I appreciate your answer , Some how when we are not able to get the path by plugins so we need to crate xpath manually like above @selenium guru has explained. Thanks for information, this idea of capturing xpath's by  Selenium IDE is nice and chrome has nice plugin for the same.
0
by
I use XPath Helper 1.0.13 with Chrome. To activate the plugin you should push CTRL+SHIFT+X. And if you hold the SHIFT and hover over the element you will get xpath so easy.
+2 votes
by Expert (4.6k points)
edited by

If you want to know how to make XPath of an element.So see

  • As we know every element has an element address and in HTML may be there would be <div> or may be <table> right.
  • Now you have to Right click on the element n which you have to click then click inspect element(Firebug plugin of firefox browser). Now you are able to see HTML part .
  • And let me tell you one imp thing HTML tags has some attributes like ID,NAME,CLASS etc.

we need to use them see this Example:

<div class="login">

<table class="input">

<tbody>

<tr id="inner">

<td class="text">

<a href="http://www.yoursfriends.com">Click Me</a>

</td></tr></tbody></table>

</div>

Here we are going to find XPATH of Click Me. And will use the attributes for finding XPath.

 

 table[@class="input"]/tbody/tr[@id="inner"]/td[@class=''text']/a 

I hope you got the point , Enjoy!

0
by (1.6k points)
I got your point and thanks for explanation, Now I understand the way to create xpath.
0 votes
by
Any elements can be identified in MANY different ways with XPath, not just one.

XPath plugins are useful for verifying any particular XPath expression is valid and does identify exactly the element(s) that you want.

But there is really nothing better than learning to use XPath properly and being able to write powerful expressions from scratch that replicate how a real user sees a page and are not brittle yet even help to identify visual defects and usability/user experience issues.
0 votes
by
Some advices: try to avoid locating elements via text and position in html. These are the worst locating element ways, because:
1) locating via text will fail with any minor text changing (for text labels verifications better to use separate asserts in tests)
2) all such xpaths like //*[text()='Some Text'] should be refactored after introducing I18n (Internationalization), L10n (Localization) [Your framework should be able to find all elements with different languages of UI]
3) locating elements by position in html (like "//div[3]//span[2]") will fail with any newly added div, span, etc. Only place where positions of elements are better to use is tables - to specify column number or row number.
0 votes
by
The idea that XPath is slower that CSS is old and outdated, stepping from Selenium RC, I think.

More recent 'scientific' measurements of the differences in speed show no no clear difference in speeds.

IMO, if you are using Selenium for testing purposes, speed is less important than the testing value that your choice of locator expression adds to your tests.
0 votes
by
I would suggest not to use XPath to find the web elements because it is very slow as it works in both directions forward and backward. Best to use CSSLocator/ID/ClassName as they are faster and never slow your selenium script. If you still looking for XPath then use FirePath an extension to Firefox.

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated