【问题标题】:Selenium WebDriver: findElement() in each WebElement from List<WebElement> always returns contents of first elementSelenium WebDriver:List<WebElement> 中每个 WebElement 中的 findElement() 始终返回第一个元素的内容
【发布时间】:2015-06-08 00:56:07
【问题描述】:
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 '解析'的网页有很多代码。我只需要处理其中包含产品网格的一小部分。 对于populateProductList() 调用,我传递了包含所有产品的DOM 的结果部分。 (在 Chrome 中运行 XPath 会返回预期的 all_products 节点。)

在该方法中,我将产品拆分为 25 个单独的 WebElement,即产品块。 (这里我也确认它在 Chrome 中工作并返回节点列表,每个节点都包含产品数据)

接下来我想遍历结果列表并将每个WebElement 传递到为我初始化ProductProduct() 构造函数中。 在此之前,我运行一个小测试并打印出产品块(参见测试 1);在每个迭代中打印出单独的块。

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

问题:这次测试只返回 FIRST 产品的每个迭代的 url/title 对。

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

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

我认为这看起来像是引用的问题,但我无法提出任何建议或在线找到解决方案。

对此有什么帮助吗?谢谢!

java 1.7.0_15、Selenium 2.45.0 和 FF 37

【问题讨论】:

  • @alecxe 这是一个承诺,一旦产品被分配了结果,它就不会被修改,如果这是问题,删除它并不能解决问题。

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

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

在 selenium 中的 xpath 表达式下方表示您正在寻找文档中 可以在任何地方 的匹配元素。不像你想的那样相对于父母!!

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

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

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

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

现在你让它搜索相对于父元素匹配的子元素。

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

【讨论】:

  • 谢谢你,Vinoth! 我不知道// 会离开父母。 . 成功了。
猜你喜欢
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2019-12-13
  • 2015-10-18
  • 2018-08-26
  • 2011-12-21
  • 1970-01-01
相关资源
最近更新 更多