java - 如何使用不同的搜索创建元素的 Xpath ( cssSelector / tag / ClassName )
问题描述
我想使用不同的 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>
我想根据那个“活动类”在哪里,点击它下面的表格。知道怎么做吗?
我想要的简短版本:
使用 cssSelector 查找元素
获取此元素的 Xpath <- 问题
使用编辑的 xpath 单击它
我想 GET XPATH OF LOCATED ELEMENT ,而不是使用 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;
}
}
推荐阅读
- html - HTML解析和Dom树构建
- mongodb - pyspark 中的 mongo 查询以提取大于某个日期的数据,该日期是字符串 ISODate 格式
- python - 用于提取节号和描述的正则表达式
- c++ - 在 C++ 中隐藏第 3 方库中的回调原型
- android - Android 工作室 java - Toast
- c++ - 在 C++ 中为 2 个字符串实现通用字符计数,但在 2 个字符串上的相同 for 循环中接收到截然不同的字符计数
- flutter - 如何在颤动的不同点击中路由到相同的页面但具有不同的功能?
- python - 在将用户输入转换为适合密码后移动字母
- android - 如何使用 InputField 小部件将“.com”按钮添加到键盘
- c++ - 从 Visual Studio 2017 迁移到 2019,/MP 标志出现问题并实现 32 位构建?