Selenium WebDriver:List<WebElement> 中的每个 WebElement 中的 findElement() 始终是第一个元素的 returns 内容

Selenium WebDriver: findElement() in each WebElement from List<WebElement> always returns contents of first element

Page page = new Page();
page.populateProductList( driver.findElement( By.xpath("//div[@id='my_25_products']") ) );

class Page
{
    public final static String ALL_PRODUCTS_PATTERN = "//div[@id='all_products']";

    private List<Product> productList = new ArrayList<>();

    public void populateProductList(final WebElement pProductSectionElement)
    {
        final List<WebElement> productElements = pProductSectionElement.findElements(By.xpath(ALL_PRODUCTS_PATTERN));

        for ( WebElement productElement : productElements ) {
            // test 1 - works 
            // System.out.println( "Product block: " + productElement.getText() );
            final Product product = new Product(productElement);
            // test 2 - wrong
            // System.out.println( "Title: " + product.getUrl() );
            // System.out.println( "Url: " + product.getTitle() );
            productList.add(product);
        }

        // test 3 - works
        //System.out.println(productElements.get(0).findElement(Product.URL_PATTERN).getAttribute("href"));
        //System.out.println(productElements.get(1).findElement(Product.URL_PATTERN).getAttribute("href"));
    }
}

class Product
{
    public final static String URL_PATTERN = "//div[@class='url']/a";
    public final static String TITLE_PATTERN = "//div[@class='title']";

    private String url;
    private String title;

    public Product(final WebElement productElement)
    {
        url = productElement.findElement(By.xpath(URL_PATTERN)).getAttribute("href");
        title = productElement.findElement(By.xpath(TITLE_PATTERN)).getText();
    }

    /* ... */
}

我尝试使用 Selenium 'parse' 的网页有很多代码。我只需要处理其中包含产品网格的一小部分。 对于 populateProductList() 调用,我传递了包含所有产品的 DOM 的结果部分。 (运行 Chrome returns 中的 XPath 是预期的 all_products 节点。)

在那个方法中,我将产品分成 25 个单独的 WebElements,即产品块。 (这里我也确认在 Chrome 和 returns 节点列表中工作,每个节点都包含产品数据)

接下来,我想遍历结果列表并将每个 WebElement 传递给 Product() 构造函数,后者为我初始化 Product。 在我这样做之前,我 运行 进行了一个小测试并打印出产品块(参见测试 1);每次迭代都会打印出单独的块。

执行产品分配后(再次,xpath 在 Chrome 中确认)我 运行 另一个测试(参见测试 2)。

问题:这次测试 returns 每次迭代仅测试来自第一个产品的 url/title 对。

除其他外,我尝试将 ProductfindElement() 调用移动到循环中,但仍然遇到同样的问题。接下来,我尝试 运行ning a findElement**s**() 并对结果执行 get(i).getAttribute("href");这次它正确地返回了单个产品的 URL(见测试 3)。

然后当我在循环内对单个 productElement 执行 findElements(URL_PATTERN) 时,它神奇地 returns 所有产品网址...这意味着 findElement() 总是returns 25 个产品集中的第一个产品,而我希望 WebElement 只包含一个产品。

我认为这看起来像是参考文献的问题,但我无法想出任何办法或在网上找到解决方案。

有什么帮助吗?谢谢!

java 1.7.0_15、Selenium 2.45.0 和 FF 37

问题出在产品定位器的 XPATH 中。

在 selenium 中的 xpath 表达式下方意味着您正在寻找一个匹配的元素,该元素 可以在文档中的任何地方。与你想的 parent 没有关系!!

//div[@class='url']/a

这就是为什么它总是 returns 相同的第一个元素。

因此,为了使其相对于 parent 元素,它应该如下所示。 (在 // 之前只是一个 .)

public final static String URL_PATTERN = ".//div[@class='url']/a";
public final static String TITLE_PATTERN = ".//div[@class='title']";

现在让它搜索相对于 parent.

匹配的 child 元素

selenium 中的 XPATH 工作原理如下。

/a/b/c   --> Absolute - from the root

//a/b    --> Matching element which can be anywhere in the document (even outside the parent).

.//a/b   --> Matching element inside the given parent