我試圖自動化的Web應用程序有一個診斷工具,允許ping到網站。它在具有表結構的框中提供輸出(全部包含在內)。硒tbody文本提取
我使用Selenium WebDriver和Java對其進行自動化編程。它的結構作爲JUnit 4測試和使用的webdriver(不硒RC,但較新的一個)
這裏是什麼樣子:
<tr>
<td style="font-family:Arial;font-size:11px;"></td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;"> </td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">PING ds-any-fp3-real.wa1.b.yahoo.com (98.138.253.109) 56(84) bytes of data.</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=1 ttl=53 time=81.9 ms</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=2 ttl=53 time=148 ms</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=4 ttl=53 time=143 ms</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;"></td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">--- ds-any-fp3-real.wa1.b.yahoo.com ping statistics ---</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">5 packets transmitted, 3 received, 40% packet loss, time 4012ms</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;">rtt min/avg/max/mdev = 81.917/124.763/148.373/30.349 ms</td>
</tr>
<tr>
<td style="font-family:Arial;font-size:11px;"></td>
</tr>
</tbody>
這裏是什麼樣子的網頁上:
PING ds-any-fp3-real.wa1.b.yahoo.com (98.138.253.109) 56(84) bytes of data.
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=1 ttl=53 time=81.9 ms
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=2 ttl=53 time=148 ms
64 bytes from ir1.fp.vip.ne1.yahoo.com (98.138.253.109): icmp_req=4 ttl=53 time=143 ms
--- ds-any-fp3-real.wa1.b.yahoo.com ping statistics ---
5 packets transmitted, 3 received, 40% packet loss, time 4012ms
rtt min/avg/max/mdev = 81.917/124.763/148.373/30.349 ms
我需要解析使用硒webdriver的這段文字,並通過JUnit測試如果ping成功(如果數據包已經丟失並不重要),我需要提取一個IP地址爲好。
有沒有什麼辦法可以提取頁面源的特定部分(也許,以某種複雜的方式使用driver.getPageSource()或通過xpath找到這個塊然後調用getText()?),然後解析它讓IP出去?我想它的方式是folows:
String IP = "";
String textToParse = //Here, we should have a way to get the string that would contain IP.
String tokenSeparators = "()"; // since our IP is enclosed by brackets
String tokens[] = textToParse.split(tokenSeparators);
for(int i = 0; i<tokens.length; i++){
if(tokens[i].matches("^[1-9]?[1-9]?[1-9]?\\.[1-9]?[1-9]?[1-9]?\\.[1-9]?[1-9]?[1-9]?\\.[1-9]?[1-9]?[1-9]?$")){ // IP regexp
IP = tokens[i]
}
}
讓我知道如何提取,我需要解析和是否有我的代碼錯誤(例如,如果我的正則表達式是正確的)
文本將不勝感激任何幫助!
這的確是解決問題的好方法,會試試看。 – worldpart