gpt4 book ai didi

java - 使用 selenium webdriver 查找表行号

转载 作者:行者123 更新时间:2023-12-01 09:06:50 25 4
gpt4 key购买 nike

我有一个表,需要查找特定的行号。在本例中,我对第二行 (2nd tr) 感兴趣。

这是 HTML:

<table>
<thead>
<tbody>
<tr class="classes mico_models_classes_7 listRowWhite">
<tr class="classes mico_models_classes_8 listRowDark">
<td style="width:130px;">9:00am - 10:30am</td>
<td class="noprint enrolledFull" style="width:70px;height:40px;text-align:center;"> </td>
<td style="width:200px;">
<a class="eventName" data-eventdescription="Very long spin class" data-eventname="Spinning 90 min" href="javascript://">Spinning 90 min</a>
<br>
with
<a class="classesEmployeeName" title="John Doe" href="javascript://" data-employeeid="5117">John Doe</a>
</td>
<td style="text-align:right;width:72px;">0 of 20</td>
<td>Spin Class</td>
</tr>
<tr class="classes mico_models_classes_9 listRowWhite">
<tr class="classes mico_models_classes_10 listRowDark">
</tbody>
</table>

不幸的是,以下代码为 dataRowIndex 返回 null:

String classTittle = "Spinning 90 min";
String dataRowIndex = driver.findElement(By.cssSelector("[data-eventname='" + classTitle + "']")).getAttribute("rowIndex");

最佳答案

您可以通过迭代 tr HTML 元素并对其进行计数来解决此问题。您还可以在迭代时访问它们。

List<WebElement> element = driver.findElements(By.cssSelector("tr"));

int row = 0;

for( WebElement w : element){

String elemText = w.getText();

System.out.println(elemText);

String clickText = "Spinning 90 min";

if(elemText.contains(clickText)){

w.click(); //do something with the element

System.out.println("Text in row " + row + " is " + clickText + " so i clicked it!");
}

System.out.println("this was row " + row + "\n");

row++;
}

将产生:

this was row 0

9:00am - 10:30am Spinning 90 min
with John Doe 0 of 20 Spin Class
Text in row 1 is Spinning 90 min so i clicked it!
this was row 1


this was row 2


this was row 3

稍后您可能希望将特定逻辑封装在方法中。希望这有帮助^^-d

关于java - 使用 selenium webdriver 查找表行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41202876/

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