Selenium tbody文本提取

时间:2012-09-26 15:53:38

标签: selenium webdriver

我尝试自动化的Web应用程序有一个允许ping网站的诊断工具。它在一个具有表结构的框中提供输出(所有这些都包括在内)。

我正在使用Selenium WebDriver和Java对其进行自动编程。它被构造为JUnit 4测试并使用WebDriver(不是Selenium 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

我需要使用Selenium WebDriver解析此文本并在ping成功时传递JUnit测试(如果数据包丢失无关紧要),我还需要提取IP地址。

有没有办法可以提取页面源的特定部分(可能是以某种复杂的方式使用driver.getPageSource()或者通过xpath找到这个部分然后调用getText()?)然后解析它以获取IP输出?我尝试的方式如下:

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]
    }

}

让我知道如何提取我需要解析的文本以及我的代码中是否存在错误(例如,如果我的正则表达式是正确的)

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

List<WebElement> allTds=driver.findElements(By.cssSelector("td[style*='font-family:Arial;font-size:11px;']");
String allTdText[]=new String[allTds.size()];
int i=0;
for(WebElement eachTd:allTds)
 {
    allTdText[i++]=eachTd.getText();
 }

通过使用上面的逻辑,您将获得String数组中的所有td标签数据。之后,将每个arraye元素解析为您的要求。

示例

字符串a =“来自ir1.fp.vip.ne1.yahoo.com的64字节(98.138.253.109):icmp_req = 1 ttl = 53 time = 81.9 ms”;

单独获取IP地址

的System.out.println(a.substring(a.indexOf( “(”)+ 1,a.indexOf( “)”)));

它将返回 98.138.253.109