Let we try to understand how to identify Xpath of element with examples.
New Test | ||
Command | Target | Value |
open | http://www.wikipedia.org/ | |
type | id=searchInput | ID Example |
or
New Test | ||
Command | Target | Value |
open | http://www.wikipedia.org/ | |
type | name=search | Name Example |
Xpath in selenium Tutorial
Now if you want to identify same element (input textbox) with xpath then you can use any of the bellow given syntax in to the target column with type command in above example.
New Test | ||
Command | Target | Value |
open | http://www.wikipedia.org/ | |
type | xpath= //body/div[3]/form/fieldset /input[2] |
Xpath Example1 |
xpath=//body/div[3]/form/fieldset/*[last()-3] //// Here /*[last()-3] describes the 4th upper node(input[2]) from last node.
New Test | ||
Command | Target | Value |
open | http://www.wikipedia.org/ | |
type | xpath= //body/div[3]/form/fieldset /input[last()-2] |
Xpath Example2 |
New Test | ||
Command | Target | Value |
open | http://www.wikipedia.org/ | |
type | xpath= //body/div[3]/form/fieldset /input[@type=’search’] |
Xpath Example3
|
New Test | ||
Command | Target | Value |
open | http://www.wikipedia.org/ | |
type | xpath= //body/div[3]/form/fieldset /input[@accesskey=’F’] |
Xpath Example4
|
11. Using starts-with keyword
xpath=//input[starts-with(@type, “s”)] //// In this example, It will find input node with attribute is ‘type’ and its value is starting with ‘s’ (here it will get type = ‘search’).
12. Using OR (|) condition with xpath
xpath=//input[@accesskey=’F’] | //input[@id=’searchInput’]
xpath=//input[@accesskey=’F’ or @id=’searchInput’] //// In both these example, it will find input text box with accesskey=’F’ or @id=’searchInput’. If any one found then it will locate it. Very useful when elements appears alternatively.
13. Using wildcard * with to finding element xpath
xpath=//*[@accesskey=’F’]
14. Finding nth child element of parent
xpath=//body/*[3]/form/fieldset/*[2] ////This xpath is for search text box. Here, /*[3] describes the 3rd child element of body which is div[3]. Same way *[2] describes the 2nd child element of fieldset which is input[2]
All above examples are for input text box. Now let me write Xpath for drop down.
8. xpath=//body/div[3]/form/fieldset/select[count(*)>1]
New Test | ||
Command | Target | Value |
open | http://www.wikipedia.org/ | |
select | xpath=//div[@class=’search-container’]/descendant::select[position()=1] | label=English
|
//a[@href=’//meta.wikimedia.org/wiki/List_of_Wikipedias’] ////This xpath example will find link with given URL (//meta.wikimedia.org/wiki/List_of_Wikipedias) on the page.
2. Finding xpath of element with no child
xpath=//img[count(*)=0] ////This xpath is for wikipedia text logo which is display on top of the page. This xpath will find that image element which have not any child element. Here image node is last and it has not any child element.
xpath=//div[2]/descendant::img[count(*)=0] //// This xpath is for wikipedia logo image which is display under logo text.
16 thoughts on “Selenium Xpath Tutorials – Identifying xpath for element with examples to use in selenium”
Unquestionably believe that which you said. Your favorite justification appeared to be on the
net the easiest thing to be aware of. I say to you, I definitely get irked while people think about worries that they just don't know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side-effects , people could take a signal. Will probably be back to get more. Thanks
Here is my webpage … java regex builder ()
Nice tutorials
Best tut , way ahead of w3 . Thanx a tone !!
helpful… 🙂
how will be the table look like, if the senario is to select a date from the dropdown and show the result. the date value may change.
thank you for above info…..how do i find xpath for varying names ….
send me html tags hierarchy
Please help me to find XPATH for such type of critical situation, hope to get a relavent links and helpfull info……..thank you
please help me …….
Your element's xpath = //div[@id='block-views-nodequeue-1-block']/div/div/div/div/div/div/ul/li[9]/div[2]/div/div/span
{Note : Wait till page loaded successfully and then use above given xpath to find your text}
it is really great! Thank you for sharing helpful info.
Thanks for the tutorial,awesome is it.
Thanks for handy things
Simply Awesome!
Nice post to understand xpath from html tree
hello