[英]How to get text next to tag with xpath
我有以下HTML代碼
<HTML> <HEAD><TITLE>CTS_Status_20170908_085307</TITLE></HEAD> <BODY> <font face=arial> <b>CTS Status Report</b> <br><br> <table border=0> <tr> <tr><td>Start time:</td><td>2017-09-06 18:31:28</td></tr> <tr><td>Current time:</td><td>2017-09-08 08:53:07</td></tr> </table> <br> Last DB maintenance record: N/A<br><br> Up time: 1 days, 14 hours, 21 minutes, 38 seconds<br><br> Transactions served: 2718<br><br> Sessions created from start: 29<br> Current sessions count: -16<br><br> DB timeouts count: 3<br> DB disconnections count: 51<br><br> Transaction info: <br>
我正在嘗試獲取有關的信息
Up time: 1 days, 14 hours, 21 minutes, 38 seconds
我正在使用以下xpath
driver.findElement(By.xpath("/html/body/font/text()[2]")).getText());
但我得到以下錯誤
xpath表達式“ / html / body / font / text()[2]”的結果為:[對象文本]。 它應該是一個元素。
有人可以幫我嗎?
您只能使用硒而不是文本節點來獲取元素
你應該做這個
driver.findElement(By.xpath("/html/body/font")).getText());
然后使用正則表達式提取所需的數據。 或者您可以使用我在下面做的基於JavaScript的方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.