我如何检查数据表是否包含我想要的值,并可能单击链接
How can I check if the Data table contains the value I want and possibly click on the link?
我正在使用web驱动程序,需要检查数据表是否包含我想要的值,并可能点击链接?
我想应该可以用xpath吧?
:我有一个web应用程序,有数据表与3列,其中
<div id="studyResultsId">
<tbody><tr>
<td><a href="/portal/study/studyAction!view.action?studyId=STUDY0000222">Using Automation</a></td>
</tr>
<tr>
<td><a href="/portal/study/studyAction!view.action?studyId=STUDY0000281">Using Design</a></td>
</tr>
<tr>
<td><a href="/portal/study/studyAction!view.action?studyId=STUDY0000272">Using Development</a></td>
</tr>
</tbody>
我尝试了以下操作,但没有成功:
String abc = driver.findElement(By.xpath(".//*[@id='studyResultsId']/div/table/tbody/tr/td")).getText();
//Retrieving the data from the Td of table in to a string
if(abc.contains("Automation")) {
System.out.println("contains Automation");
} else {
System.out.println("does not contains Automation");
}
}
根据你的html,我想先谈谈你的xpath,
driver.findElement(By.xpath(".//*[@id='studyResultsId']/div/table/tbody/tr/td")).getText();
下一行中的字符串'foo'是您可以通过上面的xpath得到的。
<div id="studyResultsId'><div><table><tbody><tr><td>foo</td></tr></tbody></table></div></div>
回到你的html。基本上当你通过id='studyResultsId'搜索时,你已经访问了第一个div标签。所以不需要第二个'/div'了。然后你试着找到'td',是的,在当前的情况下,你得到了第一个'td'元素。但是正如您所看到的,所有的标签和都没有文本。这是标签a谁有文本。所以你需要存档标签a并遍历它。下面的代码是我的建议
//Initilize your webdriver first
List<WebElement> wl = driver.findElements(By.xpath("//div[@id='studyResultsId']//a"));
for(int i=0; i<wl.size(); i++) {
WebElement current = wl.get(i);
if(current.getText().contains("Automation")) {
System.out.println("Current tag '" + current.getText() + "' has text Automation");
} else {
System.out.println("Current tag '" + current.getText() + "' has no text Automation");
}
}
j.l u和sideshowbarker已经为您的XPath表达式提供了简明的建议,我将拒绝提供其他建议,但是为了简洁起见,这里是我如何使用其他定位器定位所需的链接WebElement:
首先,我将使用它的id属性定位表WebElement:
WebElement table = driver.findElement(By.id("studyResultsId"));
现在有两种方法可以用来定位所需的链接:
By Partial Link Text -
// This will return all link WebElements within the table // that have partial matching visible text. List<WebElement> matchingLinks = table.findElements(By.partialLinkText("Automation"));
按标签名称-
// This will return all link WebElements within the table List<WebElement> tableLinks = table.findElements(By.tagName("a"));
要识别这些链接元素中哪些包含文本"Automation",您可以使用标准的Java表达式,如下所示:
List<WebElement> matchingLinks = new ArrayList<>(); for (WebElement link : tableLinks) { if (link.contains("Automation")) { matchingLinks.add(link); } }
你可以根据需要使用matchingLinks
列表中的任意一个。
相关文章:
- 当链接单击-jQuery时,第二层下拉列表将隐藏
- 在聊天窗口中检测链接单击事件
- 超链接单击以加载新页面并执行JavaScript
- 超链接单击未启动
- 取消绑定 e.prevent默认 从另一个函数转发到链接单击
- 在链接单击时执行客户端脚本
- 链接单击事件不起作用
- 为什么我在链接单击时进行 jquery/ajax 调用时无法获取值
- 为什么第二个链接单击重定向到部分不是对话框
- 如何在编辑链接单击时首次加载页面时保留值
- 禁用链接单击,直到页面加载完毕
- 记录链接单击,然后保存到本地存储
- 通过动态超链接单击事件的调用方法传递对象或字符串
- 对链接单击后出现的隐藏信息进行刮擦解析
- 使用 jquery 根据链接单击切换 DIV 可见性
- Javascript文本链接单击以选择下拉选项值(onChange?
- 在链接单击上创建一个对话框,javascript第2部分
- javascript中的递归循环,基于链接单击的增量
- 在链接单击事件上返回true是't工作
- 链接单击上的Javascript和Jquery无冲突功能