Skip to main content
Share Your Experience: Take the 2024 Developer Survey
21 votes

How to handle dynamic changing ID's In XPath?

At this situation, We have lots of options- Option 1: Look for any other attribute which Is not changing every time In that div node like name, class etc. So If this div node has class attribute then ...
Bharat Mane's user avatar
  • 6,775
12 votes

How to handle dynamic changing ID's In XPath?

Use contains() or starts-with() function in XPath: Solution: With contains(): //div[contains(@id,'post-body')]/div[1]/form[1]/input[1] With starts-with(): //div[starts-with(@id,'post-body')]/div[...
Sagar007's user avatar
  • 1,340
11 votes

What do I need to do if dropdown takes more time to populate 1st element from dropdown list?

Do you think that time is the problem here? If so try to increase the time you are providing in Thread.sleep(); and see it that works out for you. If its not the time, then did you try to find out ...
Lucy Jac's user avatar
  • 129
10 votes

How to write XPath if I have 'apostrophe' in my XPath element?

The character ' is an apostrophe. The colon character is :. Selenium unfortunately only supports XPath 1.0, so this is tricky. You can do it by escaping the ' with a backslash like this: By.xpath("//...
Michael Kay's user avatar
9 votes

How to handle dynamic changing ID's In XPath?

Use other elements and attributes for partial and combination matches Generally in these cases of database backed and changing ID's in web element selectors your options are to look for one or more ...
Michael Durrant's user avatar
8 votes
Accepted

How to find the first occurrence of a field that exists in multiple places

In XPath there are a couple of ways you could try this. Match by text (can be partial text too) (//label[contains(.,'Missing Applicant')])[1] Match by tag (if that helps isolate the label from ...
NotInventedHere's user avatar
7 votes
Accepted

How should I find the XPath using selenium webdriver and protractor

Avoid the use of 'Absolute XPath' if possible in your coding. If your element is having with class, ID, name & Type attributes then try to locate the element with the use of Relative XPath. You ...
Bharat Mane's user avatar
  • 6,775
7 votes
Accepted

How to find a button using both xpath and button text?

Use below command for finding the button via text: driver.findElementsByXPath("//button[contains(text(),'Text in your webpage')]"); This will work to capture all the 3 buttons with the same class ...
Prasanna venkatesh's user avatar
7 votes
Accepted

XPATH : text()='BUY NOW' is working but not contains(text(),'BUY NOW')

It is an interesting question. Let me share my findings. First of all //button[contains(text(),'BUY NOW')] doesn't really look up the element in Chrome dev tools. But //button[contains(.,'BUY NOW')] ...
Alexey R.'s user avatar
  • 11.6k
6 votes
Accepted

How to find element by class name or xPath?

can you please try the options below: By Xpath: button[@type='submit'] button[@class='btn btn-success'] button[@type='submit'][@class='btn btn-success'] By css selector: button[type='submit'] ...
Yu Zhang's user avatar
  • 9,942
6 votes
Accepted

What is the hierarchy to use for finding elements - ID, class, name, xpath?

My main criteria is readability and maintainability. Speed has never been as issue for me. There's other parts of the test frameworks I use that have much more significant speed issues. The basic ...
Michael Durrant's user avatar
6 votes

Xpath for sign out button

There are much better ways to locate the element aside from using XPath, don't focus on XPath too much - this is, generally speaking, the slowest and the least readable way to locate elements. I ...
alecxe's user avatar
  • 11.4k
6 votes

How to access a Hyper link using XPath

Whenever you are trying to locate Hyperlink Element & you have '/a' attribute, just forget about the XPath method and try using linkText element Locator. <html> <head> <title&...
Bharat Mane's user avatar
  • 6,775
6 votes

Same Xpath expression returns different results

Because in your first example all your span elements reside on the same level so that they all have different positions. However in your second example each span resides within its own container (<...
Alexey R.'s user avatar
  • 11.6k
6 votes

Element not found in a runtime

This could be a timing issue(as most of the scenario you already discussed) when you're going to find it would not be present at that time on DOM, so you should try using WebDriverWait to wait until ...
Nitin Rastogi's user avatar
6 votes

locating element with one class out of multiple classes inside a div using xpath in selenium

You should use contains: equals validates the attribute value is exactly equal to given value. As the attribute class value is "ng-untouched ng-pristine ng-invalid igx-input-group__input" ...
PDHide's user avatar
  • 11.1k
5 votes
Accepted

Unable to click button Selenium Webdriver 3

Try this xpath //*[contains(@class,'btn btn-primary btn-lg btn-block pink button') and contains(@tabindex,'4')] The xpath basically means : any element that has class attribute containing 'btn btn-...
BountyHunter's user avatar
5 votes

How to click a link when more than 1 exists?

When there are multiple instances and there is no other way to distinguish them* you can refer to the instance index, i.e. 'which one'. CSS a.instanceLink:nth-child(1) or a.instanceLink:nth-child(...
Michael Durrant's user avatar
5 votes
Accepted

how to handle if xpath is same for multiple drop down

It might be issue of same level HTML code. Solution : 1. For First Dropdown : (//div[@name='expiryDate']/div/a/div[2]/i)[1] 2. For Second Dropdown : (//div[@name='expiryDate']/div/a/div[2]/i)[2] ...
Sagar007's user avatar
  • 1,340
5 votes

How to handle web element(enable button) having same Xpath?

In my case, How I worked on this same scenario: Suppose if you have 3 buttons with the same XPath-like in my DOM: <input type="submit" id="button" value="Edit"/> <input type="submit" id="...
Bharat Mane's user avatar
  • 6,775
5 votes

How to access a Hyper link using XPath

As far as i know xpath doesn't depends on any browser. Make sure you have created correct xpath it will work. Second : It's depend on your tag which attribute it has and how efficient you are in ...
NarendraR's user avatar
  • 1,573
5 votes

How to write XPath if my ID is changing continuously?

Same like posted here. //*[starts-with(@id,'post-')]/h3/a/time OR //*[contains(@id,'post-')]/h3/a/time try with other attributes which are not changing continuously.
Bharat Mane's user avatar
  • 6,775
5 votes

How to find XPath inside <td>

Your text is dynamic, but it always contains .xlsx. So you can find the element with partial text matching. Try this: By.xpath("//td[contains(text(),'xlsx')]") Or more specifically: By.xpath("//td[...
Shahid's user avatar
  • 624
5 votes
Accepted

How to write XPath under tag: strong

What you expect to query is actually placed not "under" strong tag, but "next to" strong tag. There is the text nodes that you should take. Hence you should take all the text nodes having string as ...
Alexey R.'s user avatar
  • 11.6k
5 votes

How to find a button using both xpath and button text?

Using contains in xpath, for me is tricky. Tests are flaky with this approach. The better way is to use: //button[text()='Some text'] Now, you compare/test the text of the button with expected text.
Zhivko.Kostadinov's user avatar
5 votes

Selenium web driver - how to select child elements

Using xpath to locate an element works regardless of the parents of the element. It doesn't matter if the parent is an li or a span, as long as your identifier is unique. So if you are using a CSS ...
Michael Durrant's user avatar
5 votes
Accepted

Which Xpath is more suitable?

The two paths might give the same result on one sample document, but they will give different results on other documents. The one that is best is the one that gives the right results over a whole ...
Michael Kay's user avatar
4 votes

How to locate XPath for given tag?

You may use this- Xpath1= //a[contains(.,'Create a Rediffmail account')] OR Xpath2=//a[contains(@href,'details')] OR Xpath3=.//*[@id='toprightinfo']/a[2] Or you try with other Locator ...
Bharat Mane's user avatar
  • 6,775
4 votes
Accepted

Dynamic XPath in Selenium - which input field keeps changing

For the password you could try: //input[@type='password'][not(@style)] For the username : //input[@type='text'][not(@style)] This will select all input elements where the type attribute has the ...
jpjwolli's user avatar
  • 487
4 votes

How to write XPath if I have 'apostrophe' in my XPath element?

To make an apostrophe that's in XPath be interpreted literally, you can use the concat function that's defined in the W3C XPath recommendation. Notes about this function: 1. Its format is as follows (...
User253489's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible