Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a row number for a particular element Selenium?

Tags:

java

selenium

There is a table with multiple rows.

I am locating an element of this table using sccSelector

WebElement myElement = driver.findElement(By.cssSelector...........);

This element belongs to some table row, let's say row 5. How can I get this row's number?

int myElementRowNumber = ???
like image 810
Andrei Vasilev Avatar asked Mar 22 '23 00:03

Andrei Vasilev


1 Answers

Use rows = myElement.findElement(By.xpath("ancestor::table[1]")) to locate the table. Then you can use table.findElements( By.xpath("./tbody/tr")) to get all rows.

Use another row = myElement.findElement(By.xpath("ancestor::tr[1]")) to get the row element.

Now it's simply:

int myElementRowNumber = rows.indexOf( row );
like image 96
Aaron Digulla Avatar answered Apr 06 '23 03:04

Aaron Digulla