如何使用selenium(JAVA)按列名和行索引自动化表格单元格中的链接 [英] How to automate click link in table cell by column name and row index using selenium (JAVA)

查看:187
本文介绍了如何使用selenium(JAVA)按列名和行索引自动化表格单元格中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过提供列名称行索引来在表格单元格中执行点击链接的通用方法。

I want a generic approach to perform click link in table cell by providing column name and row index.

可能有多种类型的HTML表结构,但我需要通用函数,它在每个看起来像通用HTML表的表中执行操作。

There may be multiple type of HTML table structure, but I need generic function which perform action in every table which looks like a generic HTML table.

例如。下面列出了一些通用表: -

For eg. following some generic tables are define :-

1- 第一个表结构

 <table>
 <tr>
 <th>column1</th><th>column2</th><th>column3</th>
 </tr>
 <tr>
 <td><a href="">Link1</a></td><td><a href="">Link2</a></td><td><a href="">Link2</a></td>
 </tr>
 </table>

2- 第二个表结构

 <table>
 <tr>
 <td>column1</td><th>column2</th><td>column3</td>
 </tr>
 <tr>
 <td><a href="">Link1</a></td><th><a href="">Link2</a></th><td><a href="">Link2</a></td>
 </tr>
 </table>

3- 第三表结构

 <table>
 <tr>
 <td>column1</td><td>column2</td><td>column3</td>
 </tr>
 <tr>
 <td><a href="">Link1</a></td><td><a href="">Link2</a></td><td><a href="">Link2</a></td>
 </tr>
 </table>

这里如果我提供两个参数,例如。

Here If I provide two parameter for eg.

String columnName = "column2", int rowIndex = 1;

然后我的通用功能执行点击链接 Link2 在表格单元格中所有表格中的2,1。

then My generic function perform click link Link2 in table cell at 2,1 in all tables.

我可以使用 WebDriver 找到该表格,如下所示: -

I can find the table using WebDriver as follows :-

 WebDriverWait wait = new WebDriverWait(driver, 10000);
 WebElement tableElment = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table")));

但我不知道如何制作一个通用函数来按我的意愿处理这个表。

But I do not know how to make a generic function to process this table as I want.

请帮我在 Java 中创建一个通用函数来实现这个任务。

Please help me to create a generic function in Java to achieve this task.

WebDriver 中使用 JavascriptExecutor 也是可以接受的..

Using JavascriptExecutor in WebDriver is also acceptable..

伪代码算法实现这一点也是可以接受的..

Pseudo code or Algorithm to achieve this is also acceptable..

提前致谢...:)

推荐答案

你可以得到列的索引匹配目标标题,然后返回目标行/列中的链接。
我将使用一段JavaScript来执行此任务:

You could get the index of the column matching the targeted header and then return the link in the targeted row/column. I would use a piece of JavaScript for this task:

static WebElement getTableLink(WebElement table, String column, int row) {
    JavascriptExecutor js = (JavascriptExecutor)((RemoteWebElement)table).getWrappedDriver();

    WebElement link = (WebElement)js.executeScript(
        "var rows = arguments[0].rows, header = arguments[1], iRow = arguments[2];      " +
        "var iCol = [].findIndex.call(rows[0].cells, (td) => td.textContent == header); " +
        "return rows[iRow].cells[iCol].querySelector('a');                              " ,
        table, column, row);

    return link;
}

用法:

// get the table
WebElement tableElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table")));

// click the link in column "column2", row 1
getTableLink(tableElement, "column2", 1).click();

这篇关于如何使用selenium(JAVA)按列名和行索引自动化表格单元格中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆