Selenium WebElement xpath Java

I am trying to navigate a webpage using xpath and I come up with some mixed results. This is what I use:

driver.findElement(By.xpath("//div[contains(@class, 'x-grid3-cell-inner x-grid3-col-0')]"));

This really works fine, but the problem I am facing with this is:

<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 this xpath in my Selenium test, I always get the first div. How to edit my xpath to retrieve the 4th div (92301) or another div that is not the first on the list?

+5
source share
2 answers

Use this XPath (//div[contains(@class, 'x-grid3-cell-inner x-grid3-col-0')])[4]to get 4 thdiv .

To find divwhich contains text 92301, use this XPath:

//div[contains(text(), '92301')]
+8
source

WebElement.findElements():

driver.findElements(By.xpath(...)).get(3)

xpath :

driver.findElement(By.xpath("(//div[contains(@class, 'x-grid3-cell-inner x-grid3-col-0')])[4]"));
+3

All Articles