在SeleniumJava中提取文本br标记

在SeleniumJava中提取文本br标记,java,selenium-webdriver,Java,Selenium Webdriver,我需要获得文本“德克萨斯州美国”和“伊利诺伊州美国”,以便在字符串匹配后单击文本上方的超链接。我试过以下方法,但没能做到。这里有人能帮忙吗 findElement(By.xpath("//html/body/font/nobr")).getAttribute("innerHTML") findElement(By.xpath("//html/body/font/nobr")).getAttribute("textContent") findElement(By.xpath("//html/bod

我需要获得文本“德克萨斯州美国”和“伊利诺伊州美国”,以便在字符串匹配后单击文本上方的超链接。我试过以下方法,但没能做到。这里有人能帮忙吗

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()


要识别文本德克萨斯州美国并调用位于文本上方的超链接上的
单击()
,您可以使用:

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

要识别文本伊利诺伊州美国并在文本上方的超链接上调用
单击()
,您可以使用:

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

您得到的异常是什么?对于上面的findelement列表,我可以看到数据,但有时我得到findelement的方法抛出“org.openqa.selenium.InvalidSelectorException”异常(By.xpath(“//html/body/font/nobr/[following sibling::node()”)抱歉,我尝试过,但我得到的方法抛出了“org.openqa.selenium.NoSuchElementException”异常。@Y5288地址为org.openqa.selenium.NoSuchElementException检查此讨论@Y5288,欢迎!如果它解决了您的问题,您可以