• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / How to write Dynamic XPath in Selenium

How to write Dynamic XPath in Selenium

January 2, 2019 by Mukesh Otwani 173 Comments

Text method in Selenium XPath

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.

Dynamic XPath in Selenium

Using multiple attribute

//tagname[@attribute1=’value1’][attribute2=’value2’]



//a[@id=’id1’][@name=’namevalue1’]

//img[@src=’’][@href=’’]

 

XPath in Selenium1

 

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.

Dynamic Xpath in Selenium

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.

Dynamic XPath in Selenium

 

Using Following node

Xpath/following::again-ur-regular-path

//input[@id=’’]/following::input[1]

//a[@href=’’]/following::a[1]

//img[@src=’’]/following::img[1]

 Dynamic Xpath in Selenium

Using preceding node

Xpath/preceding::again-ur-regular-path

//input[@id=’’]/ preceding::input[1]

//a[@href=’’]/ preceding::a[1]

//img[@src=’’]/ preceding::img[1]

Dynamic Xpath in Selenium

Absolute XPath method

1-/html/head/body/div/input

 

Absolute-xpath

Absolute-xpath1

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 in Selenium XPath

 

Text method with contains

Syntax- tagname[contains(text(),’partial text which you want to search ‘)]

Example – //*[contains(text(),’Employee Distribution by Subunit’)]

Text method with Contains method in Selenium XPath

 

                                               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

 

Filed Under: Basic Selenium Tagged With: Xpath

Reader Interactions

Comments

  1. Manish sewlikar says

    November 19, 2024 at 9:00 PM

    Hi Mukesh,

    Do you any blog or video-related CSS selectors?

    Reply
    • Mukesh Otwani says

      December 20, 2024 at 11:53 AM

      Hi Manish,
      You can check my blog and video for CSS Selector
      https://learn-automation.com/write-dynamic-css-selector-in-selenium/

      Reply
  2. RANJAN V says

    July 15, 2021 at 11:29 AM

    Nice Tutorial)))

    Reply
  3. Narahari says

    July 12, 2020 at 8:44 PM

    Detailed documentation with images. Very clear notes to note for dynamic xpath.
    Thanks a lot Mukesh ji

    Reply
    • Mukesh Otwani says

      July 13, 2020 at 10:45 AM

      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…:)

      Reply
    • kishore says

      January 1, 2022 at 6:06 PM

      can u please help me how to write xpath for three dot button in an application

      Reply
      • Mukesh Otwani says

        January 3, 2022 at 5:16 PM

        Hi Kishore,

        Kindly share app URL for the same.

        Reply
  4. Hitesh Chaudhari says

    March 24, 2020 at 12:14 PM

    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

    Reply
    • Mukesh Otwani says

      March 25, 2020 at 8:24 PM

      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]

      Reply
  5. Nikhil says

    September 8, 2019 at 6:29 PM

    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

    Reply
    • Mukesh Otwani says

      September 11, 2019 at 1:18 PM

      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

      Reply
  6. Anil Rautela says

    September 4, 2019 at 12:39 PM

    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.

    Reply
    • Mukesh Otwani says

      September 4, 2019 at 5:45 PM

      Hi Anil,

      String VisibleText = “organization”;
      Then use it as driver.findElement(By.xpath(“//div[contains(text(),’”+VisibleText+”’)]”)).click();

      Reply
  7. Pooja says

    August 14, 2019 at 3:31 PM

    Hi,

    How to find xpath for the following case which contains href and class ng-ngbinding alone

    Reply
    • Mukesh Otwani says

      August 14, 2019 at 5:23 PM

      Hi Pooja,

      Go with //*[@href=’‘ and @ng-binding=’‘]

      Reply
      • pooja says

        August 14, 2019 at 6:15 PM

        Thank you 🙂

        Reply
        • Mukesh Otwani says

          August 14, 2019 at 11:45 PM

          You’re welcome…:)

          Reply
  8. Shivang Pathak says

    July 30, 2019 at 8:19 AM

    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

    Reply
    • Mukesh Otwani says

      July 30, 2019 at 7:39 PM

      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.

      Reply
  9. viki says

    May 14, 2019 at 7:06 PM

    how can I capture the xpath by mouse click in chrome

    Reply
    • Mukesh Otwani says

      May 15, 2019 at 6:10 PM

      Hi Viki,

      Could you please elaborate it…

      Reply
  10. Jyothi says

    April 25, 2019 at 6:42 PM

    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..

    Reply
    • Mukesh Otwani says

      April 29, 2019 at 1:49 PM

      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.

      Reply
  11. kaustubha says

    April 19, 2019 at 4:25 PM

    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.

    Reply
    • Mukesh Otwani says

      April 23, 2019 at 12:31 AM

      Hi Kaustubha, Actions class can help you to automate all keyboard events.

      Reply
  12. SATVEER BAJWA says

    April 5, 2019 at 9:07 PM

    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.

    Reply
    • Mukesh Otwani says

      April 5, 2019 at 11:56 PM

      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.

      Reply
      • SATVEER BAJWA says

        April 6, 2019 at 1:52 AM

        Okay Thanks a lot Mukesh. I will try to find solution . Thanks again

        Reply
  13. cyrus says

    March 19, 2019 at 2:21 PM

    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’)]

    Reply
    • Mukesh Otwani says

      March 20, 2019 at 1:55 PM

      Thanks Vijay 🙂 I will update my post as well.

      Reply
  14. Satwik says

    March 11, 2019 at 6:17 PM

    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!

    Reply
    • Mukesh Otwani says

      March 11, 2019 at 9:33 PM

      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.

      Reply
  15. Satwik says

    March 8, 2019 at 9:54 PM

    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?

    Reply
    • Mukesh Otwani says

      March 11, 2019 at 12:07 PM

      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

      Reply
  16. Jeby says

    February 10, 2019 at 3:19 AM

    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?

    Reply
    • Mukesh Otwani says

      February 12, 2019 at 6:27 PM

      Hi Jeby,

      This xpath looks good only. Kindly check your code once again…

      Reply
  17. Anjana says

    February 8, 2019 at 11:22 AM

    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.

    Reply
    • Mukesh Otwani says

      February 9, 2019 at 9:45 PM

      Hi Anjana,

      You are always welcome…:)

      Reply
  18. Anjana says

    February 7, 2019 at 11:56 AM

    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?

    Reply
    • Mukesh Otwani says

      February 8, 2019 at 10:51 AM

      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

      Reply
  19. Ashwini says

    February 2, 2019 at 4:53 PM

    Hi Mukesh,
    This tutorial is really very helpful and covered all the important topics. But I need more clarification on Relative and Absolute Xpath.

    Reply
  20. Shabana says

    January 21, 2019 at 2:55 PM

    Thank you so much for ur beautiful explanation. it’s helping me a lot……

    Reply
    • Mukesh Otwani says

      January 23, 2019 at 1:32 PM

      Hi Shabana,

      Thanks for your appreciation. Stay tuned to my new blog posts…:)

      Reply
  21. Jayshree Gadhave says

    January 10, 2019 at 6:56 PM

    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.

    Reply
    • Mukesh Otwani says

      January 13, 2019 at 11:30 PM

      Hi Jayshree,

      You can use ChroPath addon for firefox

      Reply
  22. Mandeep says

    February 15, 2017 at 3:48 PM

    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

    Reply
    • Mukesh Otwani says

      February 15, 2017 at 5:35 PM

      Hi Mandeep,

      Please mention the url correctly because mentioned url is not pointing to correct page.

      Reply
  23. savita says

    February 9, 2017 at 5:40 PM

    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.

    Reply
    • Mukesh Otwani says

      February 9, 2017 at 6:54 PM

      Hi Savita,

      Very soon I’ll upload a video tutorial for this.

      Reply
  24. vinod singh says

    February 8, 2017 at 3:19 PM

    Hi Mukesh,
    This post is amazing and very helpful. Can I write xpath in IE or Chrome browser like we do in Firefox browser?

    Reply
    • Mukesh Otwani says

      February 8, 2017 at 3:53 PM

      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/

      Reply
  25. vyshak says

    February 6, 2017 at 10:45 AM

    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:

    Reply
    • Mukesh Otwani says

      February 6, 2017 at 11:26 AM

      Hi Vyshakh,

      I can’t see any html stuffs over here.

      Reply
  26. Naira says

    January 29, 2017 at 8:26 PM

    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

    Reply
    • Mukesh Otwani says

      January 30, 2017 at 9:05 AM

      Hi Maruti,

      text() function always looks for exact matching of text while contains(text(),”) doesn’t look for exact matching text.

      Reply
  27. vinod singh says

    January 19, 2017 at 11:16 AM

    Hi Mukesh,

    Thank you for such a nice blog. it is really helpful for me.

    Reply
    • Mukesh Otwani says

      January 19, 2017 at 11:25 AM

      Hi Vinod,

      Thanks for your comments.

      Reply
  28. Lee says

    January 14, 2017 at 3:18 AM

    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

    Reply
    • Mukesh Otwani says

      January 14, 2017 at 12:23 PM

      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.

      Reply
  29. aridevi says

    January 11, 2017 at 8:28 AM

    Very Informative and nice blog

    Reply
    • Mukesh Otwani says

      January 11, 2017 at 9:02 AM

      Hi Aridevi,

      Thanks for your comments.

      Reply
  30. Ravi Gavkare says

    January 8, 2017 at 10:31 PM

    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.

    Reply
    • Mukesh Otwani says

      January 9, 2017 at 11:31 PM

      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.

      Reply
  31. Sandeep Rapaka says

    December 29, 2016 at 5:42 PM

    Hi Mukesh
    Very helpful thank you for your blog.

    Reply
  32. Kevin R. Mariano says

    December 27, 2016 at 10:54 PM

    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 ?

    Reply
    • Mukesh Otwani says

      December 28, 2016 at 11:18 AM

      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.

      Reply
  33. Aklakh Ahmad says

    November 20, 2016 at 5:14 AM

    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

    Reply
    • Mukesh Otwani says

      November 23, 2016 at 3:20 PM

      Hi Aklakh, You can use text and indexing as well if multiple matches are found.

      Reply
  34. Ankit Nigam says

    October 22, 2016 at 12:35 PM

    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?

    Reply
    • Mukesh Otwani says

      October 25, 2016 at 2:09 PM

      Hi Ankit,

      Any example for this?

      Reply
  35. Vikas says

    October 21, 2016 at 1:55 PM

    Hi Mukesh

    Great tutorials. Keep up the good work man. Cheers.

    Reply
    • Mukesh Otwani says

      October 25, 2016 at 5:45 PM

      Thanks Vikas 🙂

      Reply
  36. Devi says

    October 20, 2016 at 3:35 PM

    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.

    Reply
    • Mukesh Otwani says

      October 20, 2016 at 5:36 PM

      You can automate below CRM application

      http://enterprise.demo.orangehrmlive.com/symfony/web/index.php/auth/login

      Reply
  37. Mukesh Otwani says

    October 20, 2016 at 2:19 PM

    Google verification and Capcha code cant be automated

    Reply
  38. sangeeta says

    October 19, 2016 at 12:10 PM

    very happy …it helped me
    thank you mukesh

    Reply
    • sangeeta says

      October 19, 2016 at 12:33 PM

      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

      Reply
      • Mukesh Otwani says

        October 20, 2016 at 2:23 PM

        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.

        Reply
    • Mukesh Otwani says

      October 20, 2016 at 2:14 PM

      Cheers

      Reply
  39. Ranjith Samalla says

    October 7, 2016 at 2:42 PM

    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

    Reply
    • Mukesh Otwani says

      October 7, 2016 at 7:21 PM

      This is perfect one match //img[@class=’mmt_header_logo’]//following::*[text()=’flights’]

      Reply
    • Atish Kadu says

      October 15, 2016 at 4:23 PM

      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

      Reply
      • Ranjith Billakanti says

        October 17, 2016 at 10:03 AM

        Thanks Atish.

        Reply
  40. Rajani says

    October 4, 2016 at 3:40 PM

    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

    Reply
    • Mukesh Otwani says

      October 4, 2016 at 4:02 PM

      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.

      Reply
  41. Sridhar says

    September 28, 2016 at 5:04 PM

    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??

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 10:39 AM

      Hey,

      In this case you can use Index as well driver.findElements(By.xpath(“ur_xpath”)).get(0).click();

      Reply
    • Sridhar says

      October 13, 2016 at 12:54 PM

      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”));
      }

      Reply
      • Mukesh Otwani says

        October 13, 2016 at 4:43 PM

        Hi Sridhar,

        Try below

        old System.out.println(we.getAttribute(“innerText”));

        new System.out.println(we.getAttribute(“innerHTML”));

        Reply
  42. swamil says

    September 27, 2016 at 12:00 PM

    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 .

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 10:51 AM

      Will try my best for upload rest.

      Reply
  43. Gaurav Khurana says

    September 21, 2016 at 11:10 PM

    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

    Reply
    • Mukesh Otwani says

      September 23, 2016 at 2:49 AM

      Hi Gaurav,

      I already have this kindly check http://learn-automation.com/write-dynamic-css-selector-in-selenium/

      Reply
  44. pooja pilkhwal says

    September 20, 2016 at 2:50 PM

    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 .

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 2:25 PM

      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?

      Reply
  45. Vaishali says

    September 8, 2016 at 6:53 PM

    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.

    Reply
    • Mukesh Otwani says

      September 9, 2016 at 10:25 AM

      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========"); } }

      Reply
      • bhaskar says

        September 19, 2016 at 4:09 PM

        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 .

        Reply
        • Mukesh Otwani says

          September 29, 2016 at 2:53 PM

          This will help Bhaskar https://www.youtube.com/watch?v=juKRybHPMwE

          Reply
  46. Ranjith Billakanti says

    August 28, 2016 at 1:01 PM

    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.

    Reply
    • Mukesh Otwani says

      September 8, 2016 at 1:35 AM

      Hi Ranjith,

      This will help https://plus.google.com/+Mukeshotwani/posts/bPDKPZTWdoj

      Reply
      • Ranjith Billakanti says

        September 8, 2016 at 10:38 AM

        Thanks Mukesh.

        Reply
        • Mukesh Otwani says

          September 9, 2016 at 10:31 AM

          Most welcome Ranjith

          Reply
  47. suraj says

    August 25, 2016 at 11:49 AM

    Add text method also it will be helpful.
    //*[text=’my account’]
    //a[contains(text(),’logout’)]

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 10:26 PM

      Hi Suraj,

      Correct I will add. Thanks

      Reply
  48. VDS Anusha says

    August 21, 2016 at 7:19 PM

    Thank you so much mukesh for uploading this video. It’s really awesome, after watching this video i got clearance o xpath

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:28 PM

      Thanks Anusha, I am glad to know it helped you 🙂

      Reply
  49. ashok mandadi says

    August 17, 2016 at 1:32 PM

    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.

    Reply
    • Mukesh Otwani says

      August 18, 2016 at 9:33 AM

      Hi Ashok,

      Yes I have planned for manual testing as well. I will try my best to upload soon.

      Reply
  50. Neeraj Attri says

    August 11, 2016 at 12:45 PM

    Hi Mukesh Otwani,
    //tagname[@attribute1=’value1’][attribute2=’value2’] i think second attribute should start with @ .

    Reply
    • Mukesh Otwani says

      August 18, 2016 at 9:57 AM

      Yes Neeraj correct let me update the same.

      Reply
  51. Ashim Paudel says

    July 28, 2016 at 2:23 AM

    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….

    Reply
    • Mukesh Otwani says

      August 2, 2016 at 12:41 PM

      Hey Ashim,

      Cheers 🙂 and I am glad to know that you liked xpath post

      Reply
  52. sudhr says

    July 22, 2016 at 9:21 PM

    //a[contains(.,’Selenium’)])[1] plz explain this?

    Reply
    • Mukesh Otwani says

      July 25, 2016 at 12:12 PM

      It means find all elements which contains Selenium and get first element.

      Reply
  53. Divya says

    July 19, 2016 at 5:51 PM

    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

    Reply
    • Mukesh Otwani says

      July 20, 2016 at 1:51 PM

      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/

      Reply
  54. lalitha says

    July 11, 2016 at 8:59 PM

    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 🙂

    Reply
    • Mukesh Otwani says

      July 12, 2016 at 4:53 PM

      Thanks Lalitha 🙂

      Reply
  55. Soniya Vijaywargi says

    July 4, 2016 at 9:18 PM

    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.

    Reply
    • Mukesh Otwani says

      July 8, 2016 at 11:34 AM

      Hey Soniya,

      Your most welcome and Please let me know if any help required from my side 🙂

      Reply
  56. Arun says

    June 18, 2016 at 12:47 PM

    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

    Reply
    • Mukesh Otwani says

      June 20, 2016 at 12:09 PM

      I will share doc with you that will contain all the answer

      Reply
      • sandeep says

        August 3, 2016 at 3:35 AM

        Hi Mukesh ,can you please share that doc with me also(sandeepmalik340@gmail.com) . Thanks in advance

        Reply
        • Mukesh Otwani says

          August 5, 2016 at 12:03 PM

          Hey Sandeep,

          join my email list and you will get answers in your email 🙂

          Reply
      • Naveen says

        August 24, 2016 at 3:29 PM

        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:)

        Reply
        • Mukesh Otwani says

          August 27, 2016 at 11:07 PM

          Sure will send.

          Reply
  57. Sanjeev Gautam says

    June 13, 2016 at 10:57 AM

    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

    Reply
    • Mukesh Otwani says

      June 14, 2016 at 9:13 PM

      Hey Sanjeev,

      Above site has some issues.
      Kindly use below url for demos.

      Reply
  58. Satheesh says

    June 9, 2016 at 4:46 PM

    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

    Reply
    • Mukesh Otwani says

      June 14, 2016 at 9:19 PM

      Hey Satheesh,

      Chances are high that element will be inside frame, kindly cross check.

      Reply
  59. Anika Mittal says

    May 27, 2016 at 2:55 PM

    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.

    Reply
    • Mukesh Otwani says

      May 30, 2016 at 12:33 AM

      It works for me always.

      Reply
  60. sharayu says

    April 25, 2016 at 12:44 AM

    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.

    Reply
    • Mukesh Otwani says

      April 26, 2016 at 5:27 PM

      any other example?

      Reply
  61. Mayank says

    April 20, 2016 at 11:24 PM

    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

    Reply
    • Mukesh Otwani says

      April 21, 2016 at 12:20 PM

      HI Mayank

      Try below xpath

      //select[contains(@class,’item-quantity customized’)]

      Reply
  62. aakash says

    April 6, 2016 at 12:55 PM

    Hey I want to pick all packages on http://www.tripfactory.com/.can anyone provide me the possible xpath?

    Reply
    • Mukesh Otwani says

      April 10, 2016 at 12:22 PM

      Hi Akash,

      is it fixed?

      Reply
  63. Abhijeet says

    March 18, 2016 at 4:44 PM

    Hi Mukesh,
    How we use Ends-with in xpath?

    Reply
    • Mukesh Otwani says

      March 18, 2016 at 5:37 PM

      Hi Abhijeet

      use contains in this case.

      Reply
  64. Prasad says

    March 7, 2016 at 5:27 PM

    Really awesom article.
    Thank you for sharing information.

    Reply
    • Mukesh Otwani says

      March 11, 2016 at 6:40 PM

      Thanks Prasad

      Reply
  65. prathap says

    February 26, 2016 at 2:45 PM

    Thanks a lot for your great effort .it is understandable very easily.Seriously it is an ultimate blog liked it

    Thanks a lot
    Prathap

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 2:09 PM

      Thank you pratap. I am glad to know that you liked it. Keep visiting.

      Reply
  66. Sandhya says

    February 11, 2016 at 11:01 AM

    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

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:49 PM

      Hi Sandhya,

      where you are getting stuck please let me know. Using AutoIT and Sikuli we can do.

      Reply
  67. satish narayana kakimukkala says

    January 31, 2016 at 12:17 AM

    i got clear idea about xpaths……….. thank u so much mukesh otwani sir..

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:25 PM

      Hi Satish,

      Thank you. Please share with your friends as well.

      Reply
  68. Jai Prakash Keswani says

    January 21, 2016 at 6:43 PM

    Is it a correct xpath ?

    .//a[@id=’top’][contains(@href, ’add/product/1/’)]

    Reply
    • Mukesh Otwani says

      January 21, 2016 at 8:10 PM

      Hi Jai,

      Check if it is highlighting element then its correct.

      Reply
  69. yashika says

    January 8, 2016 at 3:44 AM

    Hi Mukesh,

    Thanks for sharing your intense knowledge about it.

    Thanks,
    Yashika.

    Reply
    • Mukesh Otwani says

      January 9, 2016 at 5:23 PM

      Thanks Yashika,keep visiting and check other articles too.

      Reply
    • Nagaraj Byndoor says

      May 18, 2016 at 10:38 AM

      Hi Mukesh,

      Thanks for sharing knowldge.

      Thanks,
      Nagaraj

      Reply
      • Mukesh Otwani says

        May 19, 2016 at 4:26 AM

        Your most welcome Nagaraj

        Reply
  70. bhaskar says

    January 7, 2016 at 8:05 PM

    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.

    Reply
    • Mukesh Otwani says

      January 9, 2016 at 5:34 PM

      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.

      Reply
  71. bhaskar says

    January 7, 2016 at 8:01 PM

    is there any way to write x path only by seeing the element

    Reply
    • Mukesh Otwani says

      January 9, 2016 at 5:34 PM

      Yes but for some element not for all.

      Reply
  72. Anjali Aswani says

    January 5, 2016 at 1:13 PM

    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

    Reply
    • Mukesh Otwani says

      January 6, 2016 at 12:51 AM

      Hi Anjali,

      Thank you glad to know you liked it 🙂

      Keep visiting

      Reply
  73. Srikanth says

    December 11, 2015 at 4:21 PM

    It was good intormative…Thanks

    Reply
    • Mukesh Otwani says

      December 12, 2015 at 3:46 PM

      Thanks Srikanth 🙂

      Reply
  74. Sandeep says

    December 11, 2015 at 1:50 PM

    Slower Pace and example based session. I have gone 1 step up in my xpath knowledge. thanks 🙂

    Reply
    • Mukesh Otwani says

      December 12, 2015 at 3:48 PM

      Thanks Sandeep 🙂 Glad to know it helped you. Keep visting.

      Happy weekend.

      Reply
  75. Nur says

    December 8, 2015 at 11:34 PM

    nice one man, keep going on

    Reply
    • Mukesh Otwani says

      December 9, 2015 at 12:19 AM

      thnx Nur

      Reply
  76. sairam says

    October 29, 2015 at 4:06 PM

    Hello Mukesh,

    The tutorials you posted was very useful and very informative.

    Reply
    • Mukesh Otwani says

      October 29, 2015 at 6:27 PM

      thnx Sairam

      Reply
  77. Omar says

    October 26, 2015 at 11:51 PM

    very informative.
    Thank you

    Reply
    • Mukesh Otwani says

      October 27, 2015 at 10:48 PM

      Hi Omar,

      Thanks 🙂

      Reply
  78. Satyanarayan Panigrahi says

    May 2, 2015 at 11:02 AM

    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.

    Reply
    • Mukesh Otwani says

      May 2, 2015 at 5:18 PM

      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 🙂

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?