我想使用不同的 cssSelector/tag/ClassName 找到一个元素,并获取它的 xpath 值(更具体地说,我有一个网站,当一天发生变化时,其中一个类会更改它的类)这里是我的意思是:
<tr>
<td> 1.1.2019 </td>
<td> 2.1.2019 </td>
<td class="active"> 3.1.2019 </td>
<td> 4.1.2019 </td>
</tr>
<tr>
<td> </td>
<td> 10 </td>
<td> </td> #Here
<td> </td>
</tr>
我想根据那个“活跃类”在哪里,点击它下面的表格。知道该怎么做吗?
我想要的简短版本:
Find element using cssSelector
Get this element's Xpath <- the problem
click it using edited xpath
我想获取已定位元素的 XPATH,而不是使用 Xpath 来定位它
最佳答案
您可以通过查找所有 <td>
来找到索引。第一行中的元素并检查哪个元素具有索引
List<WebElement> columns = driver.findElements(By.xpath("//tr[td[@class='active']]/td")); # just an example, can be any other locator
int index = 0;
for (int i = 0 ; i < columns.getSize() ; i++) {
String attribute = columns.get(i).getAttribute("class")
if (attribute != null && attribute.equals("active")) {
index = i + 1;
}
}
关于java - 如何使用不同的搜索创建元素的 Xpath ( cssSelector/tag/ClassName ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54671248/