Select the item that is before the item containing the specific linkText

Using selenium, I am trying to click on an element <td>that appears immediately before an element <td>that has a linkText from "Main". I am trying to click the selected item in this image:

screen

And the element <td>that I want to click, and the one that after it is inside the same element <tr>shown in the image as having id=5.

This element corresponds to the integer line highlighted below. Notice how its linkText is "green", like the element below it. Therefore, I cannot use the xpath / css selector for the element <td>I want to click.

enter image description here

I am trying to click the highlighted green. This is why I am trying to click an element <td> in front of an element with "Main" as its linkText.

I have this code, but I don’t know how to get the element before the element that matches this code: WebElement templateFound = driver.findElement(By.linkText("Main"));

All thanks, thanks

+4
source share
2 answers

You can use the axis following-sibling:

//td[following-sibling::td[1]/a = 'Main']

Basically, this will give you an element tdthat has a link with the text "Main" inside the first next tdsibling.


, , preceding, td, td:

, node , , .

WebElement templateFound = driver.findElement(By.linkText("Main"));
templateFound.findElement(By.xpath("preceding::td"));
+2

XPATH. , td, ,

//a[.='Main']/../../td[.='Green']
+1

Source: https://habr.com/ru/post/1616994/


All Articles