Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebElement xpath Java

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?

like image 362
cbohannon Avatar asked Mar 03 '13 23:03

cbohannon


People also ask

How do you write XPath for WebElement?

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 is WebElement Selenium Java?

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.


1 Answers

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')]
like image 178
Kirill Polishchuk Avatar answered Sep 21 '22 19:09

Kirill Polishchuk