无法从HTML动态表中获取数据

时间:2013-11-21 11:54:01

标签: javascript html automated-tests selenium-webdriver

我正在使用网络驱动程序测试网络应用程序 当我输入关键字并单击搜索按钮时,我生成了这个动态表:

<div id="mainView">
<div class="table-responsive">
<table id="search_results_table" class="table table-striped table-hover table-condensed table-bordered">
    <tbody>
    <tr class="ng-scope" ng-repeat="object in objects">
    <td>
    <a href="#/en/object?company=WAR001&area=X&mu=BIST_CE466&name=TSK(BEN721JUU5)(000)">
    <b class="ng-binding">TSK(BEN721JUU5)(000)</b>
    </a>
    <p style="font-size:11px">
    </td>
    </tr>
    </tbody>
</table>

我想要做的是遍历表格并提取标签之间的值。 在上面的代码中,我想要提取的值是:TSK(BEN721JUU5)(000)

到目前为止,我已经编写了以下代码,但我不知道为什么这不会打印任何内容。

@Test
private void srch() throws MalformedURLException, IOException  {
    driver.get(TestURL);
    WebElement input1 = driver.findElement(By.id("login_form_user_input"));
    input1.sendKeys("guest");
    WebElement input2 = driver.findElement(By.id("login_form_password_input"));
    input2.sendKeys("guest");
    WebElement btn = driver.findElement(By.id("login_form_signin_button"));
    btn.click();
    WebElement w1 = driver.findElement(By.id("header_search_text_field"));
    w1.sendKeys("tsk");
    WebElement resulttable = driver.findElement(By.id("search_results_table"));
    List<WebElement> rows = resulttable.findElements(By.tagName("tr")); 
    for (WebElement row : rows) { 
            List<WebElement> cols = row.findElements(By.tagName("td")); 
            System.out.println(cols.size());
            for (int i = 0; i < cols.size(); i++) {
                    WebElement data = cols.get(i);
                    System.out.println(data.getText());
            }
    } 
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过JavaScript中的document.innerHTML代码?

这样的事,

WebElement element = driver.findElement(By.id("foo"));
String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element); 

我在Get HTML Source of WebElement in Selenium WebDriver using Python

找到了答案