java - 在 selenium JAVA 中提取文本 br 标签
问题描述
我需要获取文本“Texas-United States”和“Illinois-United States”,以便在字符串匹配后单击文本上方的超链接。我尝试了以下方法,但无法做到。任何人都可以在这里帮忙
findElement(By.xpath("//html/body/font/nobr")).getAttribute("innerHTML")
findElement(By.xpath("//html/body/font/nobr")).getAttribute("textContent")
findElement(By.xpath("//html/body/font/nobr")).getText()
findElement(By.xpath("//html/body/font/nobr/text()[preceding-sibling::br]")).getText()
findElement(By.xpath("//html/body/font/nobr/a[1]")).getText()
findElement(By.xpath("//html/body/font/nobr/a[1][following-sibling::node()[1][self::BR]]")).getAttribute("innerHTML")
下面的 HTML 供参考
<nobr>
<a target="framedetail" href="DetailServlet?com=s&cname=Bank%20of%20America%2c%20National%20Association" onclick="s_objectID="https://www.example.com/item1 this.s_oc?this.s_oc(e):true">
<img src="imgs/brcb.gif" width="8" height="8" alt="Commercial Bank" border="0">
Bank of America, National Association
</a>
<br>
Texas-United States
<br>
<a target="framedetail" href="DetailServlet?com=s&cname=Bank%20of%20America%2c%20National%20Association" onclick="s_objectID="https://www.example.com/item2 this.s_oc?this.s_oc(e):true">
<img src="imgs/brcb.gif" width="8" height="8" alt="Commercial Bank" border="0">
Bank of America, National Association
</a>
<br>
Illinois-United States
<br>
</nobr>
解决方案
如果你想点击前面的链接,你可以使用下面的代码:
findElement(By.xpath("//text()[contains(., 'Texas-United States')]/preceding-sibling::a[1]")).click()
和
findElement(By.xpath("//text()[contains(., 'Illinois-United States')]/preceding-sibling::a[1]")).click()
推荐阅读
- asp.net-mvc - 绑定到模型时如何设置默认选择的组合框?
- c# - 如何在 IIS 中使用 WebApi 2 c# 托管 Angular 应用程序?
- react-native - React Native Android 版本的 Appcenter / Fastlane 错误
- apache-kafka - KTables 在启动时如何与 Kafka 交互?
- python - 尝试除了 IndexError - 我没有得到想要的结果
- java - 从 Json 到 Object,Java
- java - 我正在尝试打印“字段”的全部值。但是它跳过了第一个输入,为什么?
- node.js - Express 服务器将带有文本字段的多部分表单数据发送到外部 api
- mysql - MySql 错误代码:1175。您正在使用安全更新模式 - 即使使用主键
- c# - 使用 Func 进行动态排序
, IOrderedQueryable >