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("//...
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 ...
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 ...
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 ...
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 ...
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')] ...
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']
...
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 ...
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&...
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 (<...
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 ...
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" ...
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(...
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-...
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]
...
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="...
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 ...
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.
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[...
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 ...
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.
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 ...
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 ...
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 ...
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 (...
4
votes
Accepted
Cannot get attribute value when using xpath
svg has a default namespace http://www.w3.org/2000/svg. You need to handle namespaces. Either with a prefix:
//*[starts-with(@id,'highcharts')]/svg:svg/svg:g[6]/svg:g[1]/svg:rect[2]
Or, with local-...
4
votes
Accepted
How to get nested web element in dynamic table using xpath
Assuming that you want to select the span element with the class check.
You need to search for the tr that contains your text and continue from there.
//tr[.//td[contains(text(),'@')]]//span[@class='...
4
votes
How should I find the XPath using selenium webdriver and protractor
An absolute XPath should start with a slash /:
'/html/body/div[2]/div[1]/div/div[1]'.
It is advised to use relative XPath like: '//div[@id="portfolioName"]'. For this to work, the id has to be ...
4
votes
How should I find the XPath using selenium webdriver and protractor
Say "No" to XPaths, to quote the Protractor style guide:
It's the slowest and most brittle locator strategy of all
Markup
is very easily subject to change and therefore xpath locators ...
4
votes
Can't send values using sendKeys function in Selenium
Because your Xpath is an absolute Xpath (that is, you start at the top of the HTML DOM and trace through all the elements), it's very fragile. If any part of the DOM above the element you're looking ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
xpath × 339selenium-webdriver × 313
automated-testing × 130
java × 70
css-selectors × 28
selenium2 × 27
python × 22
browser-automation × 17
c# × 9
page-objects × 7
selenium-webdriver-c# × 7
exception × 7
css × 7
uiautomation × 6
testng × 5
appium × 5
javascript × 4
chromedriver × 4
robotframework × 4
elements × 4
jmeter × 3
protractor × 3
selenium-ide × 3
soapui × 3
test-design × 2