简体   繁体   English

Selenium WebDriver:List中每个WebElement中的findElement() <WebElement> 总是返回第一个元素的内容

[英]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();
    }

    /* ... */
}

The webpage I am trying to 'parse' with Selenium has a lot of code. 我试图用Selenium“解析”的网页有很多代码。 I need to deal with just a smaller portion of it that contains the products grid. 我需要处理其中包含产品网格的一小部分。 To the populateProductList() call I pass the resulting portion of the DOM that contains all the products. 对于populateProductList()调用,我传递包含所有产品的DOM的结果部分。 (Running that XPath in Chrome returns the expected all_products node.) (在Chrome中运行该XPath会返回预期的all_products节点。)

In that method, I split the products into 25 individual WebElements, ie, the product blocks. 在该方法中,我将产品分成25个单独的Web元素,即产品块。 (Here I also confirm that works in Chrome and returns the list of nodes, each containing the product data) (这里我也确认它适用于Chrome并返回节点列表,每个节点都包含产品数据)

Next I want to iterate through the resulting list and pass each WebElement into the Product() constructor that initializes the Product for me. 接下来,我想迭代结果列表,并将每个WebElement传递给Product()构造函数,该构造函数为我初始化Product Before I do that I run a small test and print out the product block (see test 1); 在我这样做之前,我进行了一个小测试并打印出产品块(参见测试1); individual blocks are printed out in each iteraion. 在每个迭代中打印出单独的块。

After performing the product assignments (again, xpath confirmed in Chrome) I run another test ( see test 2 ). 执行产品分配后(再次,在Chrome中确认xpath),我运行另一个测试( 参见测试2 )。

Problem: this time the test returns only the url/title pair from the FIRST product for EACH iteration. 问题:这次测试只返回FIRST产品的url / title对进行EACH迭代。

Among other things, I tried moving the Product 's findElement() calls into the loop and still had the same problem. 除此之外,我尝试将ProductfindElement()调用移动到循环中,但仍然遇到同样的问题。 Next, I tried running a findElement**s**() and do a get(i).getAttribute("href") on the result; 接下来,我尝试运行findElement**s**()并对结果执行get(i).getAttribute("href") ; this time it correctly returned individual product URLs (see test 3). 这次它正确返回了单个产品URL(参见测试3)。

Then when I do a findElements(URL_PATTERN) on a single productElement inside the loop, and it magically returns ALL product urls... This means that findElement() always returns the first product from the set of 25 products, whereas I would expect the WebElement to contain only one product. 然后,当我做了findElements(URL_PATTERN)在单一productElement内循环,并奇迹般地返回所有产品网址......这意味着findElement()总是返回从一组的25种产品的第一款产品,而我期望WebElement只包含一个产品。

I think this looks like a problem with references, but I have not been able to come up with anything or find a solution online. 我认为这看起来像引用的问题,但我无法想出任何东西或在线找到解决方案。

Any help with this? 对此有何帮助? Thanks! 谢谢!

java 1.7.0_15, Selenium 2.45.0 and FF 37 java 1.7.0_15,Selenium 2.45.0和FF 37

The problem is in the XPATH of the Product locators. 问题出在产品定位器的XPATH中。

Below xpath expression in selenium means you are looking for a matching element which CAN BE ANYWHERE in document. 下面硒XPath表达式意味着你正在寻找它可以随时随地在文档匹配的元素。 Not relative to the parent as you are thinking!! 正如你所想的那样,与父母无关!

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

This is why it always returns the same first element. 这就是为什么它总是返回相同的第一个元素。

So, in order to make it relative to the parent element it should be as given below. 因此,为了使它相对于父元素,它应该如下所示。 (just a . before //) (只是一个。之前//)

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

Now you make it search for matching child element relative to the parent. 现在,您可以搜索相对于父元素的匹配子元素。

XPATH in selenium works as given below. 硒中的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM