0

I want to select 4 products from the products list and after selecting a product the page will load and I have given the wait for page load but still, I am getting the stale exception for click and below is my code

List<WebElement> products = getDriver().findElements(By.xpath("//button[@class='icon icon-btn icon-add'][\"+(counter + 1) + \\\"]"));

System.out.println("The products Counts  is: "+products.size());

Iterator<WebElement> iter = products.iterator();

int count=1;

while(iter.hasNext() && prodCount > count ) {
    WebElement prod = iter.next();
    prod.click(); 
    elementUtils.waitForPageLoaded(getDriver());
    count++;
    }

Getting belwo exception at prod.click stale element reference: element is not attached to the page document

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
user3538483
  • 193
  • 2
  • 3
  • 14
  • Are you getting the error on the 2nd click? – Todor Minakov Feb 04 '19 at 08:34
  • yes on second click i am getting the exception – user3538483 Feb 04 '19 at 08:52
  • If the question wasn't blindly marked as a duplicate to a generic answer that doesn't say much, I would have answered you in full. Here's a cut version - when you add the production to the cart, your page either a) reloads, or b) changes its DOM quite a lot. In both cases, the references to the elements you had from `findElements()` are now invalid - they point to objects that now simply don't exist in the DOM (they may look the same rendered, but are different objects as far as the browser is concerned). You will have to re-get them, and in doing so change your logic. – Todor Minakov Feb 04 '19 at 09:09
  • marked as duplicate but the reference one also doesn't have the answer – user3538483 Feb 04 '19 at 10:33

0 Answers0