0

有没有办法从 WebElement 中提取多个 WebElement?例如,我有以下内容:

        String filterRowXpath = "(//div[@id=\"tabItemFilters\"]//div[@id=\"RowExpGridWidgetGridPanel\"]//div[@class=\"x-grid3-body\"]//tbody)";
    List<WebElement> filterRows = driver.findElements(By.xpath(filterRowXpath));

我最终得到了一个本身包含 5 个元素的 WebElement,有没有办法让我引用元素 2 和 3?

4

2 回答 2

0

If you have something like :

<tbody>
<webelement>1<webelement/>
<webelement>2<webelement/>
<webelement>3<webelement/>
</tbody>

And you want 2 or 3, you can add [2] or [3] which is equivalent to [position()=2] or [position()=3].

So for your XPath it would be ..//tbody[position()=2 or position()=3] if you're looking for either of those positions. Note that you cannot use shorthand as in the previous statement if you are searching for multiple positions.

UPDATE:

filterRowXpath= "(((//div[@id=\"tabItemFilters\"]//div[@id=\"RowExpGridWidgetGridPanel\"]//div[@class=\"x-grid3-body\"]//tbody)[" + i + "])//td)[position()=2 or position()=3]

This will give you two node results back, instead of executing separate xpaths for each one.

List<WebElement> filterRows = driver.findElements(By.xpath(filterRowXpath));

Then apply your condition to the WebElements. You were almost there in the snippet posted in your original question.

于 2013-01-25T18:09:29.133 回答
0

这是我想出的:

    String ipFromValueXpath; 
    String ipToValueXpath;

    for(int i=1; i<=filterRows.size(); ++i) {
        ipFromValueXpath = "(((//div[@id=\"tabItemFilters\"]//div[@id=\"RowExpGridWidgetGridPanel\"]//div[@class=\"x-grid3-body\"]//tbody)[" + i + "])//td)[2]";
        ipToValueXpath = "(((//div[@id=\"tabItemFilters\"]//div[@id=\"RowExpGridWidgetGridPanel\"]//div[@class=\"x-grid3-body\"]//tbody)[" + i + "])//td)[3]";

        WebElement e1 = driver.findElement(By.xpath(ipFromValueXpath));
        String ipFromValue = e1.getText();

        WebElement e2 = driver.findElement(By.xpath(ipToValueXpath));
        String ipToValue = e2.getText();

        if(ipFromValue.equals(ipFrom) && ipToValue.equals(ipTo)) {
            e1.click();
            element = driver.findElement(By.xpath(deleteFilterButtonXpath));
            element.click();
            Thread.sleep(500);
            AdminFunctions.apply();
            return;
        }

    }

我必须获取每个元素的 Xpath,然后遍历每一行以找到匹配项。我不认为这是最有效的方法。

于 2013-01-25T19:14:22.513 回答