我有清单项目。如何通过selenium读取它们

时间:2014-01-23 16:56:07

标签: selenium selenium-webdriver listitem

<div class="validation-summary-errors" data-valmsg-summary="true">
    <ul>
        <li>Service User Number must be 6 characters</li>
        <li>PSL ID is required</li>
        <li>>Bank Account Id (was Credit AccID) is required</li>
        <li>Account Name is required</li>
        <li>Sort Code is required</li>
        <li>Account Number is required</li>
        <li>Bank Statement Narrative is required</li>
        <li>Company Name is required</li>
        <li>Contact Name is required</li>
        <li>Email Address is required</li>
        <li>Customer Services Phone Number is required</li>
        <li>Phone Number is required</li>
        <li>Address is required</li>
        <li>Reference Number Prefix is required</li>
        <li>Report Email Address is required</li>
    </ul>
</div>

我想阅读所有列表并获取它们的值。有人帮助我尽快。由于selenium不识别无序标签,因为它没有名称和id任何东西.....请帮助..

4 个答案:

答案 0 :(得分:2)

你可以这样做:

public void test() {
    WebElement ul = driver.findElement(By.cssSelector("div.validation-summary-errors ul"));

    List<WebElement> lis = ul.findElements(By.tagName("li"));

    for (WebElement li : lis) {
        // do something with li.getText();
    }
}

答案 1 :(得分:1)

为什么分两步走?我们可以在一个选择器中找到li;

public void test() {
WebElement lis = driver.findElement(By.cssSelector("div.validation-summary-errors ul>li"));

for (WebElement li : lis) {
    // do something with li.getText();
}

}

答案 2 :(得分:0)

@sircapsalot的答案很好,但是我会更改第一个CSS选择器以满足OP的要求“selenium不识别无序标签,因为它没有名称和id任何东西”

public void test() {
    WebElement ul = driver.findElement(By.cssSelector("div.validation-summary-errors ul"));

    List<WebElement> lis = ul.findElements(By.tagName("li"));

    for (WebElement li : lis) {
        // do something with li.getText();
    }
}

答案 3 :(得分:-1)

如果您的SVG数据如下所示:

pre-commit
<div id="content-data" class="col-md-12" style="height:666px;">
<svg id="pie-draw-488172" width="492" height="616"><g transform="translate(226,318)">
<text id="pie-text" cursor="default" y="-226" x="241">Compute (227,311)</text>
<text id="pie-text" cursor="default" y="-211" x="241">Database (98,005)</text>
<text id="pie-text" cursor="default" y="-196" x="241">Storage&amp;Content Delivery (69,436)</text>
<text id="pie-text" cursor="default" y="-181" x="241">Networking (30,874)</text>

<text id="pie-text" cursor="default" y="-166" x="241">Other (11,273)</text>

然后使用此

</svg></div>
 public List<IWebElement> content_data = new List<IWebElement>();