Selenium : How to Create Xpath
Target
URL: www.mouthshut.com
Target
Web element: Signup button, Login
Button
Tools used:
·
FireBug : To formulate Xpath
·
Selenium Ide : To Test the formulated Xpath
Things to remember:-
1.
Target page should be open is FireFox
2.
Selenium ide and Firebug should
be open
3.
The formulated Xpath will go into “Target” field of Selenium IDE.
4.
All Xpath which go into IDE start with “xpath=//”
5.
The page usually start with <body> , therefore the foremost parent
will always be “//Body” while
writing Full Path
6.
Html Page may be like :
<body id=”aaaaa”>
<div
id=”bbbbb”>
<div
id=”cccc”>
<a
id=”dddd” class=”1111”>
<ul
id=”eeee” target=”2222” href=”5555”>
<div
id=”ffff”>
Syntax in IDE:
Xpath=//branch/branch [ @property=’value’ or contains ( @property,
“value”) ]
In the above example :
1. Branch or Node – body , div , a ,
ul ,img
Note : Each Node value will have many
properties and many values .Therefore always start with a node value or
"*" along with its relate property and value (Ex
://a[@target='_new'] or //*[@target='_new'] )
2. Property
– id , class , target
3. Value
– aaaa , bbbb , cccc , dddd , 1111 , 2222 , eeee , ffff ,5555
Syntax
in Selenium (Eclipse Java):
( Note -"Syntax in Selenium IDE" is only used to create Xpath and the
below syntax is the usage of Xpath created using IDE )
Syntax
:
WebElement oObject_Name=Driver_Object.findElement(By.xpath("Created Xpath Goes here ,starting from
'//'"));
Example
:
oDriver=new FirefoxDriver();
oDriver.get("https://URL_Here");
WebElement
oObject=oDriver.findElement(By.xpath("//branch/branch [ @property=’value’ or contains ( @property,
“value”) ]"));
1. Using Full Path
xpath=//body/form/div[7]/div/div[2]/div/div[2]/ul/li/a
(The page
usually start with <body> , therefore the foremost parent will always be “//Body” while writing Full Path )
2.Functions
Navigation
Descendants::div[1]
ancestor::div[1]
child::div[1]
Selenium Code :
oDriver=new FirefoxDriver();
oTitle=oDriver.findElement(By.xpath("//body/form/div[7]/div/div[2]/div/div[2]/ul/li/a"));
2. Using Last()
xpath=//body/form/div[7]/div/div[2]/div/div[last()]/ul/li/a
3. Using Full Path+ @ attribute
·
xpath=//body/form/div[7]/div/div[2]/div/div[last()]/ul/li/a[@class='']
·
xpath=//body/form/div[7]/div/div[2]/div/div[last()]/ul/li/a[@style='background-color:
transparent;']
(For Login
Button)
xpath=//body/form/div[7]/div/div[2]/div/div[last()]/ul/li[2]/a[@target='_new']
4. Using only @ attribute (For Login Button)
xpath=//a[@target='_new']
(Which is
equivalent to
xpath=//body/form/div[7]/div/div[2]/div/div[last()]/ul/li[2]/a[@target='_new'] )
5. Using
Descendants
xpath=//div[@class='new-menu']/descendant::div[@class='fr']
xpath=//div[@class='new-menu']/descendant::div[@class='fr']
6. Using keywords
xpath=//a[contains(@href,
"javascript:__doPostBack(")]
xpath=//a[contains(@id,
"ctl00_ctl00_ctl00_ContentPlaceHolderHeader_ContentPlaceHolderFooter_litSignUp")]
7. And Operation
xpath=//a[contains(@id,
"ctl00") and contains(@href,"javascript:_")]
8. using position()
xpath=//div[@class='tdltaln']/div[position()=2]/ul/li
9. Using starts-with keyword
xpath=//a[starts-with(@href,
"https://graph.facebook.com")]
10. OR (|) condition
xpath=//a[@id='ctl00_ctl00_ctl00_ContentPlaceHolderHeader_ContentPlaceHolderFooter_litSignUpLink'
or contains(@href,"javascript:__doPostBack")]
xpath=//a[contains(@id,"ctl00_ctl00_ctl00_")
or contains(@href,"javascript:__doPostBack") ]
xpath=//a[contains(@href,"javascript:__doPostBack")
or contains(@id,"ctl00_ctl00_ctl00_")]
11. Going 1 step back
Traversing from child to parent use
"/.."
12. Going back
multiple steps or reverse tracing ( ancestor:: )
Ex :If user "ancestor::div" , then all the ancestor elements are highlighted with "div" node
Back Trace to first occurrence of div element
Back Trace to first occurrence of div element
//*[text()='Biological Sciences']/ancestor::div[1]
13. Going to
specfic child element from parent node:( child:: )
Simialr to just "/ut[1]"
Simialr to just "/ut[1]"
//*[text()='Biological
Sciences']/ancestor::div[1]/child::ul[1]
14. Wild Card “*”
xpath=//*[@id="ctl00_ctl00_ctl00_ContentPlaceHolderHeader_ContentPlaceHolderFooter_litSignUpLink"]
15. Nth element
xpath=//ul[@class='login']/*[1]
16. No
child
xpath=//img[count(*)=0]
17. Last Second or Last But one
xpath=//div[@class='dvaligncenter']/div[last()-1]//a
17. Last Second or Last But one
xpath=//div[@class='dvaligncenter']/div[last()-1]//a
Examples for Xpath
xpath=xpathExpression:
Locate an element using an XPath expression.
1. xpath=//img[@alt='The image alt text']
2. xpath=//table[@id='table1']//tr[4]/td[2]
3. xpath=//a[contains(@href,'#id1')]
4. xpath=//a[contains(@href,'#id1')]/@class
5. xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
6. xpath=//input[@name='name2' and @value='yes']
7. xpath=//*[text()="right"]
8. xpath=//*[@id='view']/*[not(@selected='selected')][1]
9. xpath=//*[text()='Biological Sciences']/ancestor::div[1]
10. xpath=.//*[local-name(.)='th' or local-name(.)='td']
1. xpath=//img[@alt='The image alt text']
2. xpath=//table[@id='table1']//tr[4]/td[2]
3. xpath=//a[contains(@href,'#id1')]
4. xpath=//a[contains(@href,'#id1')]/@class
5. xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
6. xpath=//input[@name='name2' and @value='yes']
7. xpath=//*[text()="right"]
8. xpath=//*[@id='view']/*[not(@selected='selected')][1]
9. xpath=//*[text()='Biological Sciences']/ancestor::div[1]
10. xpath=.//*[local-name(.)='th' or local-name(.)='td']
Xpath summary
1.Path
- Full
- Full+attribute
- Attribute
- Wild Card(*)
- Recent ancestor "/.. "
- //a[6]
- div[3][contains(@id,'dvheader')] -goto 3rd element and fetch if it has dvheader similar to AND
- position
- [Last()] //[Last[]-1]
- [Position()= 42]
- count
- text
- [starts-with(@,””)] -- Used For Dynamic Elements
- [contains(@,””)] -- Used For Dynamic Elements
- [text()=””] --text
3.Logical
- And
- or
- not
Ref:
http://software-testing-tutorials-automation.blogspot.in/2013/06/xpath-tutorials-identifying-xpath-for.html
Great site for these post and i am seeing the most of contents have useful for my Carrier.Thanks to such a useful information.Any information are commands like to share him.
ReplyDeleteSelenium Training in Chennai
Thank you, You have spent good time I guess to write this kind of good articles, there is lnk for xpath, please do read it.relative XPATH in Selenium webdriver
ReplyDelete