Welcome to Selenium tutorial, today we will see how to write Dynamic XPath in Selenium because we can’t go ahead without XPath and CSS so let’s discuss XPath different methods in detail. Selenium has 8 locators which include id, name, classname, XPath, CSS, linkText, partialLinkText and tag name which make our task easy to find elements.
Previously we have firebug and firepath in Firefox which helped us to find and locate XPath in a simple manner but now both are officially deprecated now we have very easy Chrome Extension called ChroPath which does the same job. I have a detailed video on this which talks about how to use ChroPath.
One more plugin to find XPath called XPath Checker but I love ChroPath
Even if you don’t want to use any plugin then still you can find XPath in chrome browser using the normal search box, for this as well I have a detailed video which talks about how to find XPath in chrome browser without any plugin.
Once you are done with XPath, I will highly recommend you to visit Dynamic CSS as well which is highly used in Cross-browser Testing as well.
Here is the Youtube video for Dynamic XPath
Dynamic XPath in Selenium Webdriver
Relative XPath method
Using single attribute
// tagname[@attribute-name=’value1’] Example // a [@href=’http://www.google.com’] //input[@id=’name’] //input[@name=’username’] //img[@alt=’sometext’]
And so on.
Using multiple attribute
//tagname[@attribute1=’value1’][attribute2=’value2’] //a[@id=’id1’][@name=’namevalue1’] //img[@src=’’][@href=’’]
And so on.
Using contains method
Syntax //tagname[contains(@attribute,’value1’)] //input[contains(@id,’’)] //input[contains(@name,’’)] //a[contains(@href,’’)] //img[contains(@src,’’)] //div[contains(@id,’’)]
And so on.
Using starts-with method
//tagname[starts-with(@attribute-name,’’)] //id[starts-with(@id,’’)] //a[starts-with(@href=’’)] //img[starts-with(@src=’’)] //div[starts-with(@id=’’)] //input[starts-with(@id=’’)] //button[starts-with(@id,’’)] And so on.
Using Following node
Xpath/following::again-ur-regular-path //input[@id=’’]/following::input[1] //a[@href=’’]/following::a[1] //img[@src=’’]/following::img[1]
Using preceding node
Xpath/preceding::again-ur-regular-path //input[@id=’’]/ preceding::input[1] //a[@href=’’]/ preceding::a[1] //img[@src=’’]/ preceding::img[1]
Absolute XPath method
1-/html/head/body/div/input
Text method in XPath
Syntax- tagname[text()=’text which is visible on page’]
Note- While using text() method make sure you provide the exact text else it will not match
Example //b[text()=’Admin’]
Text method with contains
Syntax- tagname[contains(text(),’partial text which you want to search ‘)]
Example – //*[contains(text(),’Employee Distribution by Subunit’)]
Relative and Absolute XPath method
//parent-xpath/absolute xpath //input[@id=’section’]/div/input
Hope you are comfortable with XPath now. Please start writing XPath from your side and let me know if you are facing any issue.
For More updates Learn Automation page
For any query join Selenium group- Selenium Group
RANJAN V says
Nice Tutorial)))
Narahari says
Detailed documentation with images. Very clear notes to note for dynamic xpath.
Thanks a lot Mukesh ji
Mukesh Otwani says
Hi Hari,
Thanks for your appreciation. Please feel free to ask your doubts and queries on my blog/youtube. I’ll try my level best to clarify it…:)
kishore says
can u please help me how to write xpath for three dot button in an application
Mukesh Otwani says
Hi Kishore,
Kindly share app URL for the same.
Hitesh Chaudhari says
Hi Mukesh,
Firsts of all thanks for the knowledge, it really helps me out with XPath. however, want your help in one scenario.
i am dealing with multiple sites in loop and using following xpath to located and get clicked “About us” link.
driver.findElement(By.xpath(“//a[text()=’About us’ or text()=’About Us’ or text()=’ABOUT US’ or text()=’about us’]”)).click();
But in one site I got three elements using this xpath. No i wanted to clicked on second one. how do I do it
Thanks,
Hitesh Chaudhari
Mukesh Otwani says
Hi Hitesh,
You can use above xpath with indexing like (//a[text()=’About us’ or text()=’About Us’ or text()=’ABOUT US’ or text()=’about us’])[1] OR (//a[text()=’About us’ or text()=’About Us’ or text()=’ABOUT US’ or text()=’about us’])[2]
Nikhil says
Hi Mukesh can you please tell me difference between css and xpath ? which is faster and why? among all 8
locator ?which we should we prefer under which scenario.
Thanks in advance
Mukesh Otwani says
Hi Nikhil,
CSS always traverse forward while xpath can traverse both
Xpath engine is different for different browsers while CSS remains same
Moreover, IE doesn’t has its native xpath engine due which CSS works works well on IE
Anil Rautela says
Hi Mukesh,
I want to pass variable to my contains(text(),’ ‘) method. For example,
String organization=excel.getOrg(“Org_Net_Name”, 1, 0);
driver.findElement(By.xpath(//div[contains(text(),’Visible Text’)]).click(); so here I dont want “Visible Text”, I want to pass “organization” which I have stored in String variable.
How will I do this? Please share the code.
Mukesh Otwani says
Hi Anil,
String VisibleText = “organization”;
Then use it as driver.findElement(By.xpath(“//div[contains(text(),’”+VisibleText+”’)]”)).click();
Pooja says
Hi,
How to find xpath for the following case which contains href and class ng-ngbinding alone
Mukesh Otwani says
Hi Pooja,
Go with //*[@href=’‘ and @ng-binding=’‘]
pooja says
Thank you 🙂
Mukesh Otwani says
You’re welcome…:)
Shivang Pathak says
Hi Mukesh,
Can you please keep more details for ” Relative and Absolute XPath method” because most of the time testers have been facing the issue to find this time of Xpath.
Thanks
Shivang
Mukesh Otwani says
Hi Shivangi,
Selection of property attributes while creation of relative xpath solely depends on stability of corresponding property as there is no strict guidelines for creation of xpaths. You will learn more sophisticated way of xpath creation as long as you keep on practising it. You can use chropath extension/addon in order to verify xpath which you are creating.
viki says
how can I capture the xpath by mouse click in chrome
Mukesh Otwani says
Hi Viki,
Could you please elaborate it…
Jyothi says
Dear sir
If I want to insert data by clicking add row and the textbook box differ by index and I want to read data from Excel..
Mukesh Otwani says
Hi Jyothi,
You can all achieve these by single looping mechanism. Customize your xpath in such a way that it should accept index value with iteration number and same should be used to read data from excel too.
kaustubha says
Hi Mukesh,
Can we do Control+f to search for something or click F3 using python3 selenium? or can we get access to all window actions or keyboard actions? If you have any solution (workable solution) then please let me know I tried my ways none of them are working.
Mukesh Otwani says
Hi Kaustubha, Actions class can help you to automate all keyboard events.
SATVEER BAJWA says
Hi Mukesh,
Thanks for your guidance, I have different scenario in my webpage there is one element called builder where we edit our pictures and add any text, stickers on it or so on , I was trying to get the xpath of textbox that let say textbox is layer on the picture. How can I locate that layer in selenium . I have been trying since long time but I am not getting any help . please guide me accordingly.
Mukesh Otwani says
Hi Satveer,
Now a days, market is full of different types of ui components and libraries so without looking and working on web ui of application, I can’t provide any solid solution for this. Follow what exception/ error you are getting and try to find solution for same.
SATVEER BAJWA says
Okay Thanks a lot Mukesh. I will try to find solution . Thanks again
cyrus says
Hi Mukesh,
This is regarding the contains method that is used to locate element from the First Video timed at around 16:08, I guess you were writing the syntax incorrectly. I wrote the below and it found the title text:
//input[contains(@name,’post_title’)]
Mukesh Otwani says
Thanks Vijay 🙂 I will update my post as well.
Satwik says
No Mukesh my question was that in tags field in the UI that is the GUI there is also a text area so when you are writing the xpath it is showing one matching node, does that mean it is within one div tag and all the other fields like TITLE and it’s corresponding text area and TAGS and it’s corresponding text area are under independent and separate divs?
Please share me the link to this site and Uname and password Mukesh, I could not get the link. Thanks in advance!
Mukesh Otwani says
Hi Satwik,
In application which I used, there was only one textarea, all others were input box only and that was the reason it was showing one maching node. If there were more than one textareas in UI then I could gone with other attributes too.
textarea tag can come under div tag also, as it depends UI component design.
Satwik says
In the second video of xpath after content you are looking for textarea right? But the one next to TAGS label in the UI is also has a textarea, so why is it not being shown or is getting reflected in the following command? It should have shown two then right?
Mukesh Otwani says
Hi Satwik,
Good observation but let me explain this… There were two same words, content & Content. First one was attribute of id and next one was actual text. Both are different. And you should also remember one more thing which is, creation of dynamic xpath is always case sensitive
Jeby says
Hi Mukesh,
I have fetched a alphanumeric character and stored In a string. It doesn’t have any white characters when it is stored in a string. However when I try to pass it through dynamic xpath like, //*[text()='” + string +”‘], the strings takes some white char in bEgiNniNg and end and it cannot find the xpath. How can I solve the issue?
Mukesh Otwani says
Hi Jeby,
This xpath looks good only. Kindly check your code once again…
Anjana says
Thank You, Mukesh,
I did till //a[test()=’ABC’]/[1] but was not getting any matching results. I tried many times but not getting the element what I was looking for but () worked like magic.
Thank You again.
Mukesh Otwani says
Hi Anjana,
You are always welcome…:)
Anjana says
Hi Mukesh,
How can I write the XPath for the element which is n number of matching result and I want to select only the one(The first one)?
For example, There is an n number of listings and all listings are having an edit option. How can I select the first one?
Mukesh Otwani says
Hi Anjana,
Call driver.findElements which will return you list of webelements then iterate to required one
Or
Use indexing in xpath iteself like you have this dynamic xpath *//a[text()=’ABC’] which matches to multiple elements now use (*//a[text()=’ABC’])[1] to get first element
Ashwini says
Hi Mukesh,
This tutorial is really very helpful and covered all the important topics. But I need more clarification on Relative and Absolute Xpath.
Shabana says
Thank you so much for ur beautiful explanation. it’s helping me a lot……
Mukesh Otwani says
Hi Shabana,
Thanks for your appreciation. Stay tuned to my new blog posts…:)
Jayshree Gadhave says
I start with web elements locator but it found quite difficult to guess the xpath is correct or not without firepath. As latest version not support the firepath. can u suggest the way that I can use to enter xpath and on same screen it shows the correct match or not.
Mukesh Otwani says
Hi Jayshree,
You can use ChroPath addon for firefox
Mandeep says
HI, I have cliked on Edit button on my screen .But the Treaty Excess Id is changing everytime .
Please find the below Html.Could you please help me in finding xpath
edit
Mukesh Otwani says
Hi Mandeep,
Please mention the url correctly because mentioned url is not pointing to correct page.
savita says
HI mukhesh ji
You are god of selenium.Thanku so much.it helped me a aloot.Is there any way to find out xpath in IE browser.
Mukesh Otwani says
Hi Savita,
Very soon I’ll upload a video tutorial for this.
vinod singh says
Hi Mukesh,
This post is amazing and very helpful. Can I write xpath in IE or Chrome browser like we do in Firefox browser?
Mukesh Otwani says
Hi Vinod,
Yes, you can write and validate your dynamic xpath in Chrome and IE. But its not easy just like we find same using FirePath in FF.
For Chrome, 1. Enabling Developer tool, do Cntrl+F keyboard action. After this, you can see one input bx at bottom of developer tool. In that input box, test your dynamic xpath over there.
2. developer tool, enable console then type your dynamic xpath enclosed in $x() like this Sx(“//input[@value=’q’]”). Now press enter and matching results will come up in console itself
For IE, you can visit this link http://westciv.com/mri/
vyshak says
Hi Mukesh,
I just started learning selenium through ur blogs. Now i am facing issue like duplicate ID name. I want to take value from Task Owner. Can u help me???
Status:
Task Owners:
Mukesh Otwani says
Hi Vyshakh,
I can’t see any html stuffs over here.
Naira says
Hello Mukesh. Thank for Your blog, because I’m a beginner and it really helps me.
Could You please to write down a little about text() methods, because in the video above in some cases syntax was contains(text(), ‘something’), in the others contains(text()= ‘something’). I didn’t get.
Thanks
Mukesh Otwani says
Hi Maruti,
text() function always looks for exact matching of text while contains(text(),”) doesn’t look for exact matching text.
vinod singh says
Hi Mukesh,
Thank you for such a nice blog. it is really helpful for me.
Mukesh Otwani says
Hi Vinod,
Thanks for your comments.
Lee says
Hi Mukesh
Let say you have a multiple ‘Browse…” on the same page. But, the id is dynamic. Everytime you log into that session. So, how would I be able to define the correct button of “Browse…”?
The only way is by something that is calls array> How does that works?
Example:
…==$0
Mukesh Otwani says
Hi Lee,
Yes, you can use array for that. Take all locators wrt browse button inside an array and call its click action based on index. Or you go for dynamic xpath.
aridevi says
Very Informative and nice blog
Mukesh Otwani says
Hi Aridevi,
Thanks for your comments.
Ravi Gavkare says
Hello Mukesh,
Thanks for uploading such a nice and useful selenium webdriver inforamtion.
I have one query about dynamic xpath-
I have following scenario :
Click on + Add button then it will add new row in the table.
Now here whatever new row added in the table then its hard to find xpath.
Second thing i want to add multiple dynamic xpath and want to enter text.
Please suggest some solution.
Mukesh Otwani says
Hi Ravi,
If you are able to locate + button using xpath then after click on button, try dynamic xpath like //button[text()=’+’]/following::tr[1] to get first row. In similar fashion, you can proceed with other rows. This is just an example to navigate. Actual xpath on your application might differs.
Sandeep Rapaka says
Hi Mukesh
Very helpful thank you for your blog.
Kevin R. Mariano says
Hi Mukesh,
How does this work if the ID is continuously changing?
I’m talking specifically about Salesforce.
You’re method worked for the first run, no doubt. But when we got the new push(release), all the id changed and script failed.
Do you recommend any unique approach to handle dynamic changes for automating Salesforce UI with Selenium Webdriver ?
Mukesh Otwani says
Hi Kevin,
Interesting question 🙂 In this case you can take label name and then following tag. You also have to check the pattern of locator which can be changed and which can not. Based on that we need to decide which kind of xpath we will write.
Aklakh Ahmad says
Hi Mukesh,
There is multiple card in single page having unique tag name value but the place/sequence of changing dynamically. How to capture particular card?
Thanks
Mukesh Otwani says
Hi Aklakh, You can use text and indexing as well if multiple matches are found.
Ankit Nigam says
Hi Mukesh,
I need to find svg class element locator. I tried multiple combination but can not go inside the svg class. Can you please help me here how to find svg class locator?
Mukesh Otwani says
Hi Ankit,
Any example for this?
Vikas says
Hi Mukesh
Great tutorials. Keep up the good work man. Cheers.
Mukesh Otwani says
Thanks Vikas 🙂
Devi says
Hi This is Devi. Your Answers are very good. Can u please help me? related to project in selenium. And actually happens in the real time project. I know manual and selenium concepts but how to implement those concepts in real time project give and explain one project.
Mukesh Otwani says
You can automate below CRM application
http://enterprise.demo.orangehrmlive.com/symfony/web/index.php/auth/login
Mukesh Otwani says
Google verification and Capcha code cant be automated
sangeeta says
very happy …it helped me
thank you mukesh
sangeeta says
Hi mukesh …
I have a question ..
In our comapny, they told to use Automation (selenium) for regression testing first time and don’t have senior automation testers to guide . I know selenium webdriver but…how to implement all techniques in a real time project….how to verify valid and invalid values with error messages, completely.
I felt instead of writing the codes and spending the time with exceptions,it is better to do manual testing very fast………
Please guide me …
Even though I know selenium and written script, but don’t know how to use it in real time project with some techniques….
…….Everybody will explain basic execution using,find element,XPath concept,testng,Junit…..
but I want to know, how to use which technique in which situation
I don’t have hands-on experience much…
Hope you will reply….
Thank you
Mukesh Otwani says
Hey Sangeeta,
I can understand your point but before my next answer first go through below post and video
http://learn-automation.com/base-class-in-selenium-webdriver/
Once you are done then let me know.
Mukesh Otwani says
Cheers
Ranjith Samalla says
Hi Muskesh,
https://www.makemytrip.com/flights
Im trying to handle Make my trip site Flights/ Domestic tab there is no Id, name or class
i used
1)//*[contains(@href,)]
2)a[starts-with(@href=”//www.makemytrip.com/flights”)]
3) //a[@href=”//www.makemytrip.com/flights”]/following::a[1]
these are adentifying still 3 matching nodes can u pls let me how can i adentify Flight/Domestic tab
Mukesh Otwani says
This is perfect one match //img[@class=’mmt_header_logo’]//following::*[text()=’flights’]
Atish Kadu says
Hi Ranjith,
You can try this one.
1. //a[@href=’//www.makemytrip.com/flights’]
2. //a[contains(text(),’domestic’)]
If you want to fetch the href attribute then
//a[contains(text(),’domestic’)]/@href
Thanks,
Atish
Ranjith Billakanti says
Thanks Atish.
Rajani says
Hi Mukesh ,
Id’s are getting changed dynamically in My application , I.e We are Creating Multiple Users, while login in to the application we need to verify an Image so the image id is getting changed User to user then how can i proceed(Here we dont have any text area to use Conatins or Starts-With)
As shown below:
can u please suggest a solution for this
Mukesh Otwani says
Hi Rajani,
We have multiple options to check. Check if classname, alt text, position, src and so on will help you to verify the same.
Sridhar says
HI Mukesh,
How to write Relative xpath if two nodes are matching ,
Example Two nodes:
Node1:
Belagavi (IXG)
Node2:
Belagavi (IXG)
i want to click on first node , how to write relative xpath??
Mukesh Otwani says
Hey,
In this case you can use Index as well driver.findElements(By.xpath(“ur_xpath”)).get(0).click();
Sridhar says
Hi Mukesh,
I got empty string when i use gettext() method while printing text from dropdown in Chrome….. Can please help me to find solution for this? Please see the below code which i used…
ArrayList al= (ArrayList) driver.findElements(By.xpath(“//ul[@id=’DDL_Icon_listbox’]/li/*[@class=’selected-value’]”));
for(WebElement we:al)
{
System.out.println(we.getAttribute(“innerText”));
}
Mukesh Otwani says
Hi Sridhar,
Try below
old System.out.println(we.getAttribute(“innerText”));
new System.out.println(we.getAttribute(“innerHTML”));
swamil says
Hi Mukesh,
i have been following most of your Videos on You Tube and learnautomation site,your explanations are too good.
When can we expect tutorial on collections…like list,set,Queue .
Mukesh Otwani says
Will try my best for upload rest.
Gaurav Khurana says
One of the best tutorial. I am not sure why i was away from it for so many days. Found it on Quora. Thanks Keep sharing the great things.
It would be good to include CSS selector as well.
for example
css=a#id
css=a.classname
Mukesh Otwani says
Hi Gaurav,
I already have this kindly check http://learn-automation.com/write-dynamic-css-selector-in-selenium/
pooja pilkhwal says
hi mukesh
can you please tell me how to handle dynamic id .
Scenario:-create one folder under one folder upload 50 files.
every time when we upload files ,id will be change .now that time i note all file id in excel after that one -by-one file will be scheduled .so.how can handle id in code .
Mukesh Otwani says
Hi Pooja,
Yes you can need to add all concepts now 😉 Have you tried this from your side? What is the problem you are facing?
Vaishali says
Hi Mukesh,
Thanks a lot for sharing of Selenium which is so informative and very helpful.
Can you help me,
In my application the tabs which are used are dynamically changing and have to use SkipException but, i am not able to apply it.
Example :- Billing Manager, Claims Manager, Insurance and so on…
sometimes “Insurance Tab” is not in use.
Please, if you can help me, it will be a big help.
Thank You ..waiting for your reply.
Mukesh Otwani says
Hi Vaishali,
Can you try below code
@Test
public void test2()
{
boolean status=driver.findElements(By.xpath(“ur xpath will come here”)).size()<=0; if(status) { System.out.println("=====Skiping test===="); throw new SkipException("=======Tab is not enabled so skipping the test========"); } }
bhaskar says
Hello Mukesh,
I am new to selenium , I was referring you Facebook test to verify various element . I couldn’t able to verify day , month and year element. Can you please help me on this .
Mukesh Otwani says
This will help Bhaskar https://www.youtube.com/watch?v=juKRybHPMwE
Ranjith Billakanti says
Hi Mukesh,
Thanks for your posts, your posts are more informative, i am facing issues for one field to uniquely identify. could you please provide solution for this pls.
details:
Field name : Vendor
Attributes :
when i modfiy xpath as //input[@name=’C_BPartner_ID’] it is matching with 4 nodes
and tried with multiple attributes using “And” operation as well.
Mukesh Otwani says
Hi Ranjith,
This will help https://plus.google.com/+Mukeshotwani/posts/bPDKPZTWdoj
Ranjith Billakanti says
Thanks Mukesh.
Mukesh Otwani says
Most welcome Ranjith
suraj says
Add text method also it will be helpful.
//*[text=’my account’]
//a[contains(text(),’logout’)]
Mukesh Otwani says
Hi Suraj,
Correct I will add. Thanks
VDS Anusha says
Thank you so much mukesh for uploading this video. It’s really awesome, after watching this video i got clearance o xpath
Mukesh Otwani says
Thanks Anusha, I am glad to know it helped you 🙂
ashok mandadi says
Hi mukeh,
Good afternoon,
I am new in testing tools,i am learning manual+selenium but i can follow your site and i learn lot of things in your site,
Is it possible to upload manual testing then can learn very fastly like doubts something like this.
Mukesh Otwani says
Hi Ashok,
Yes I have planned for manual testing as well. I will try my best to upload soon.
Neeraj Attri says
Hi Mukesh Otwani,
//tagname[@attribute1=’value1’][attribute2=’value2’] i think second attribute should start with @ .
Mukesh Otwani says
Yes Neeraj correct let me update the same.
Ashim Paudel says
Thanks for the awesome tutorial. I have been stuck with my selendroid automation coz I could not figure out how to combine two attributes. Saved my day….
Mukesh Otwani says
Hey Ashim,
Cheers 🙂 and I am glad to know that you liked xpath post
sudhr says
//a[contains(.,’Selenium’)])[1] plz explain this?
Mukesh Otwani says
It means find all elements which contains Selenium and get first element.
Divya says
hey Mukesh
very informative blog, can you please tell me how to find the xpath of last input field using last() or count() function?
I have used following code, but it dint work
//input[@type=’checkbox’][last()]
regards
Divya
Mukesh Otwani says
Hey Divya,
There are two solutions for this.
Solution 1-
use following and then take last element using index
Solution 2-
driver.findElements(By.xpath(“//input[@type=’checkbox’]”));
This will return list of checkbox then you can perform on last checkbox
check below post for more details.
http://learn-automation.com/difference-between-findelement-and-findelements-in-selenium-webdriver/
lalitha says
Hi Mukesh,
it`s really a nice session given by you and easy to understand even.
Thanks! Keep posting nice sessions like this. presentation was so nice 🙂
Mukesh Otwani says
Thanks Lalitha 🙂
Soniya Vijaywargi says
Hi Mukesh, I am new to Selenium and I must say your blog is awesome. I am refering this blog for my learning. Keep posting!!!
Thank you so much.
Mukesh Otwani says
Hey Soniya,
Your most welcome and Please let me know if any help required from my side 🙂
Arun says
Hi Mukesh,
I am following all your blogs on selenium and it is really helpful.
I always get one question in interview. what is that challenging task you have done in selenium. I really don’t know what to answer. I am a beginner in selenium. Could you help me with some examples I can try out which quite challenging to be done in selenium
Mukesh Otwani says
I will share doc with you that will contain all the answer
sandeep says
Hi Mukesh ,can you please share that doc with me also(sandeepmalik340@gmail.com) . Thanks in advance
Mukesh Otwani says
Hey Sandeep,
join my email list and you will get answers in your email 🙂
Naveen says
Hi Mukesh,
I started referring your blog from the last week and The informations are very much useful for me. I am beginner in selenium, I am learning for the past 2 months. could you please me the doc which you shared with Arun. So that I will prepare for my interview,
Thanks!! Keep posting:)
Mukesh Otwani says
Sure will send.
Sanjeev Gautam says
Hello sir,
I can’t able to login on http://www.demosite.center/wordpress/wp-login.php
with user id: admin & Password : demo123
It’s showing 404 error
Mukesh Otwani says
Hey Sanjeev,
Above site has some issues.
Kindly use below url for demos.
Satheesh says
Hi,
Could you please help me on my problem,
When I am writing xpath in fire bug and it matches perfectly with the node. But if i am using in eclipse i am getting a exception – NoSuch Element Expection.
I Have used wait conditions as well.. Waiting for your reply.
Thanks,
Satheesh
Mukesh Otwani says
Hey Satheesh,
Chances are high that element will be inside frame, kindly cross check.
Anika Mittal says
Sir,
I wanted to know how to find xpath when your id is continuously changing. I mean last xpath doesn’t work any more. Element not found error is arising at every run.
Please help. It’s urgent.
Mukesh Otwani says
It works for me always.
sharayu says
Hi, I am trying to locate the url for topmost ad link (first line) and click on it. The ad link comes in iframe and there is no good locator for which i can find element; as all the ids keep changing for every new search and class names have space in their name which doesnt evaluate in firepath. could you please help me in locating xpath or any other locator.
Mukesh Otwani says
any other example?
Mayank says
Hi Mukesh
I found your website quite helpful in learning selenium xpaths.
Thanks for sharing such a great information.
I want to tell you that I am facing a problem while handling xpath which is dynamic.
Basically the task I am trying to perform is :
open snapdeal.com
search external hard disk
add to the cart(any hard disk)
Now when I am trying to increase the quantity of hard disk in cart and I am not able to do that.
The xpath of drop down list value keeps on changing.
I also asked few experts..they were also not able find the solution.
I request you to help me out.
Thanks
Mukesh Otwani says
HI Mayank
Try below xpath
//select[contains(@class,’item-quantity customized’)]
aakash says
Hey I want to pick all packages on http://www.tripfactory.com/.can anyone provide me the possible xpath?
Mukesh Otwani says
Hi Akash,
is it fixed?
Abhijeet says
Hi Mukesh,
How we use Ends-with in xpath?
Mukesh Otwani says
Hi Abhijeet
use contains in this case.
Prasad says
Really awesom article.
Thank you for sharing information.
Mukesh Otwani says
Thanks Prasad
prathap says
Thanks a lot for your great effort .it is understandable very easily.Seriously it is an ultimate blog liked it
Thanks a lot
Prathap
Mukesh Otwani says
Thank you pratap. I am glad to know that you liked it. Keep visiting.
Sandhya says
Hi Mukesh,
i faced this question interview.For Example in flipakart Application i choose one item and payed amount for that item.after that we will get one Reference no.i want to copy and paste that number in desktop note pad? how We can do?By using Auto it we can do it or any other procedure is there please let me know
Thank you in advance
Mukesh Otwani says
Hi Sandhya,
where you are getting stuck please let me know. Using AutoIT and Sikuli we can do.
satish narayana kakimukkala says
i got clear idea about xpaths……….. thank u so much mukesh otwani sir..
Mukesh Otwani says
Hi Satish,
Thank you. Please share with your friends as well.
Jai Prakash Keswani says
Is it a correct xpath ?
.//a[@id=’top’][contains(@href, ’add/product/1/’)]
Mukesh Otwani says
Hi Jai,
Check if it is highlighting element then its correct.
yashika says
Hi Mukesh,
Thanks for sharing your intense knowledge about it.
Thanks,
Yashika.
Mukesh Otwani says
Thanks Yashika,keep visiting and check other articles too.
Nagaraj Byndoor says
Hi Mukesh,
Thanks for sharing knowldge.
Thanks,
Nagaraj
Mukesh Otwani says
Your most welcome Nagaraj
bhaskar says
i have faced this question in the interview. in an app. there are three obj. named a,b,c,.under each object there is button named ADDTOCART .and i want to press the ADDTOCART button(under object c only.not a and b).is there any way to click on button by only seeing the element.
Mukesh Otwani says
Hi Bhaskar,
You can do using following method of xpath. Fist find add to cart button then using following get all button and then get the first one only.
bhaskar says
is there any way to write x path only by seeing the element
Mukesh Otwani says
Yes but for some element not for all.
Anjali Aswani says
Hi Mukesh
Thanks for sharing your wide knowledge in Xpath.
The way you have explained was very much easy for us to understand.
Thanks
Anjali
Mukesh Otwani says
Hi Anjali,
Thank you glad to know you liked it 🙂
Keep visiting
Srikanth says
It was good intormative…Thanks
Mukesh Otwani says
Thanks Srikanth 🙂
Sandeep says
Slower Pace and example based session. I have gone 1 step up in my xpath knowledge. thanks 🙂
Mukesh Otwani says
Thanks Sandeep 🙂 Glad to know it helped you. Keep visting.
Happy weekend.
Nur says
nice one man, keep going on
Mukesh Otwani says
thnx Nur
sairam says
Hello Mukesh,
The tutorials you posted was very useful and very informative.
Mukesh Otwani says
thnx Sairam
Omar says
very informative.
Thank you
Mukesh Otwani says
Hi Omar,
Thanks 🙂
Satyanarayan Panigrahi says
Hi Mukesh….i am a big fan of you…
Kindly share some post regarding how to handle a element using regular expression that changes regularly.
Mukesh Otwani says
Hi Satyanayan,
Thanks a ton for such a nice comment.
I generally use start-with and contains method for handling dynamic element.
Example if i have one field like username567 and when i reload page id change to username787 so in this you can use above xpath technique.
Hope its clear 🙂