title: Selenium WebDriver: findElement() in each WebElement from List<WebElement> always returns contents of first element
body: <pre><code>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&lt;Product&gt; productList = new ArrayList&lt;&gt;();
  
      public void populateProductList(final WebElement pProductSectionElement)
      {
          final List&lt;WebElement&gt; 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();
      }
  
      /* ... */
  }
  </code></pre>
  
  <p>The webpage I am trying to 'parse' with Selenium has a lot of code. I need to deal with just a smaller portion of it that contains the products grid.
  To the <code>populateProductList()</code> call I pass the resulting portion of the DOM that contains all the products.
  (Running that XPath in Chrome returns the expected all_products node.)</p>
  
  <p>In that method, I split the products into 25 individual WebElements, i.e., the product blocks.
  (Here I also confirm that works in Chrome and returns the list of nodes, each containing the product data)</p>
  
  <p>Next I want to iterate through the resulting list and pass each <code>WebElement</code> into the <code>Product()</code> constructor that initializes the <code>Product</code> for me.
  Before I do that I run a small test and print out the product block (see test 1); individual blocks are printed out in each iteraion.</p>
  
  <p>After performing the product assignments (again, xpath confirmed in Chrome) I run another test (<strong>see test 2</strong>).</p>
  
  <p><strong>Problem: this time the test returns only the url/title pair from the FIRST product for EACH iteration.</strong></p>
  
  <p>Among other things,  I tried moving the <code>Product</code>'s <code>findElement()</code> calls into the loop and still had the same problem. Next, I tried running a <code>findElement**s**()</code> and do a <code>get(i).getAttribute("href")</code> on the result; this time it correctly returned individual product URLs (see test 3). </p>
  
  <p>Then when I do a <code>findElements(URL_PATTERN)</code> on a single <code>productElement</code> inside the loop, and it magically returns ALL product urls... This means that <code>findElement()</code> always returns the first product from the set of 25 products, whereas I would expect the <code>WebElement</code> to contain only one product. </p>
  
  <p>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. </p>
  
  <p>Any help with this? Thanks!</p>
  
  <p>java 1.7.0_15, Selenium 2.45.0 and FF 37</p>