繁体   English   中英

提取硒JAVA中的文本br标签

[英]Extract text br tags in selenium JAVA

我需要获取文本“ 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&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()

要标识文本德克萨斯州-美国并在文本上方的超链接上调用click() ,可以使用:

driver.findElement(By.xpath("//nobr//br[normalize-space()='Texas-United States']//preceding::a[1]")).click();

要标识文本伊利诺伊州-美国并在文本上方放置的超链接上调用click() ,可以使用:

driver.findElement(By.xpath("//nobr//br[normalize-space()='Illinois-United States']//preceding::a[1]"))click();

请试试:

findElement(By.xpath("//html/body/font/nobr")).getAttribute("innerText")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM