[英]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&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()
要標識文本德克薩斯州-美國並在文本上方的超鏈接上調用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.