Доступ к элементу в таблице (Selenium, C#) - PullRequest
0 голосов
/ 18 февраля 2020

Я пытаюсь автоматизировать задачу на работе, и для этого требуется доступ к элементу во вложенной таблице. мне удалось получить XPath через Chrome, но я получил ошибку, из-за которой невозможно найти элемент.

Сообщение об ошибке: enter image description here

1 Ответ

0 голосов
/ 18 февраля 2020

Не используйте абсолютный xpath из chrome, так как это может быть неудачным. Когда вы работаете с таблицами, лучше всего обращаться к таблицам> tr> td. Ниже приведен пример, который поможет вам. Это может быть вашей отправной точкой, а затем, основываясь на тексте ячейки, вы можете нажать на элемент.

//find table.
WebElement mytable = driver.findElement(By.xpath("html/body/table/tbody"));

//find rows of table.
List < WebElement > rows_table = mytable.findElements(By.tagName("tr"));

//calculate no of rows In table.
int rows_count = rows_table.size();

//Loop will execute for all the rows of the table
for (int row = 0; row < rows_count; row++) {

//find columns(cells) of that specific row.
List < WebElement > Columns_row = rows_table.get(row).findElements(By.tagName("td"));
//calculate no of columns(cells) In that specific row.
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row " + row + " are " + columns_count);

//Loop will execute till the last cell of that specific row.
for (int column = 0; column < columns_count; column++) {
    //To retrieve text from the cells.
    String celltext = Columns_row.get(column).getText();
    System.out.println("Cell Value Of row number " + row + " and column number " + column + " Is " + celltext);
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...