gpt4 book ai didi

java - 在selenium JAVA中提取文本br标签

转载 作者:行者123 更新时间:2023-11-30 06:11:17 25 4
gpt4 key购买 nike

我需要获取文本“德克萨斯州-美国”和“伊利诺伊州-美国”,以便我可以在字符串匹配后单击文本上方的超链接。我尝试过以下方法但无法实现。有人可以帮忙吗

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&amp;cname=Bank%20of%20America%2c%20National%20Association" onclick="s_objectID=&quot;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">
&nbsp;Bank of America, National Association
</a>
<br>
&nbsp;&nbsp;&nbsp;Texas-United States
<br>
<a target="framedetail" href="DetailServlet?com=s&amp;cname=Bank%20of%20America%2c%20National%20Association" onclick="s_objectID=&quot;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">
&nbsp;Bank of America, National Association
</a>
<br>
&nbsp;&nbsp;&nbsp;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()

关于java - 在selenium JAVA中提取文本br标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50179652/

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