gpt4 book ai didi

java - 迭代表行并单击行内的按钮/链接

转载 作者:行者123 更新时间:2023-12-02 01:26:14 25 4
gpt4 key购买 nike

我有这个表,每一行都有一个执行功能的链接。使用我拥有的代码,它单击第一行,我想要实现的是查找项目是否等于该行中的项目,然后在找到该行后单击按钮。示例如下:

    WebElement baseTable = driver.findElement(By.xpath("//table[@id='materials']"));
List<WebElement> materialsRow = baseTable.findElements(material_table_tr);
List<WebElement> clickAction = driver.findElements(material_first_column);


try {
for (int i = 0; i < materialsRow.size(); i++) {
String name = materialsRow.get(i).getText();
Thread.sleep(500);
if (name.contains(item)) {
clickAction .get(i).click();
} else
throw new Exception("Invalid Selection");
}
} catch (Exception e) {
e.getMessage();
}

预计:item=台北101

查找名称项目相同的行。如果找到,请单击该行内的按钮。

实际:上面的代码仅点击第一行。

Please see image

HTML:

<tbody>
<tr>
<th scope="row">Burj Khalifa</th>
<td>UAE</td>
<td>Dubai</td>
<td>829m</td>
<td>2010</td>
<td>1</td>
<td><a href="#">details</a></td>
</tr>
<tr class="odd">
<th scope="row">Clock Tower Hotel</th>
<td>Saudi Arabia</td>
<td>Mecca</td>
<td>601m</td>
<td>2012</td>
<td>2</td>
<td><a href="#">details</a></td>
</tr>
<tr>
<th scope="row">Taipei 101</th>
<td>Taiwan</td>
<td>Taipei</td>
<td>509m</td>
<td>2004</td>
<td>3</td>
<td><a href="#">details</a></td>
</tr>
<tr class="odd">
<th scope="row">Financial Center</th>
<td>China</td>
<td>Shanghai</td>
<td>492m</td>
<td>2008</td>
<td>4</td>
<td><a href="#">details</a></td>
</tr>

最佳答案

您可以将代码更改为

   boolean found = false;
for (int i = 0; i < materialsRow.size(); i++) {
String name = materialsRow.get(i).getText();
Thread.sleep(500);
if (name.contains(item)) {
clickAction .get(i).click();
found = true;
break; // do not need to look anymore
}
}

if (!found) throw new Exception ("not found");

关于java - 迭代表行并单击行内的按钮/链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57583602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com