I am trying to navigate through a web page with xpath and I am coming up with some mixed results. This is what I am using:
driver.findElement(By.xpath("//div[contains(@class, 'x-grid3-cell-inner x-grid3-col-0')]"));
That actually works great but the problem I am running into this this:
<div class="x-grid3-cell-inner x-grid3-col-0" unselectable="on">92300</div>
<div class="x-grid3-cell-inner x-grid3-col-0" unselectable="on">92475</div>
<div class="x-grid3-cell-inner x-grid3-col-0" unselectable="on">92476</div>
<div class="x-grid3-cell-inner x-grid3-col-0" unselectable="on">92301</div>
<div class="x-grid3-cell-inner x-grid3-col-0" unselectable="on">92474</div>
When I run that xpath in my Selenium test I always get the first div. How do I edit my xpath to retrieve the 4th div (92301) or some other div that isn't the first one in the list?
Note: Use Ctrl+F to write XPath in the elements tab as shown below. As seen above, a simple XPath is used to locate the firstName tab. Based on the XPath syntax, first use // which means anywhere in the document. Here, input is the tag name that has an id attribute with the value “usernamereg-firstname”.
What are WebElements in Selenium? Anything that is present on the web page is a WebElement such as text box, button, etc. WebElement represents an HTML element. Selenium WebDriver encapsulates a simple form element as an object of the WebElement.
Use this XPath (//div[contains(@class, 'x-grid3-cell-inner x-grid3-col-0')])[4]
to get 4thdiv
.
To find div
which contains 92301
text, use this XPath:
//div[contains(text(), '92301')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With