My code is giving the following error "org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document" .It runs once, but when it goes to the second item, the error happens.
/**
* CONSEGUI CAPTURAR OS ITENS A SER COMPRADOS ATRAVES DO PRIMEIRO WEBELEMENT
* que basicamente fez um elemento web com a div que contem os itens
* E DEPOIS FIZ UMA LISTA DESSE PRIMEIRO WEBELEMENT
* aonde filtrei pelo ids que continham a string "add-to-cart-"
*/
WebElement prod=driver.findElement(By.id("inventory_container"));
List <WebElement> listofItems = prod.findElements(By.xpath("//button[starts-with(@id,'add-to-cart-')]"));
for(WebElement product : listofItems){
prod=driver.findElement(By.id("inventory_container"));
listofItems = prod.findElements(By.xpath("//button[starts-with(@id,'add-to-cart-')]"));
driver.findElement(By.xpath("//button[@id='add-to-cart-sauce-labs-fleece-jacket']")).click();
product.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[contains(@class, 'shopping_cart_container')]")).click();
driver.findElement(By.xpath("//button[@id='checkout']")).click();
CadastraComprador("joao", "costa", "35510");
driver.findElement(By.xpath("//*[contains(@class, 'submit-button btn btn_primary cart_button btn_action')]")).click();
comparaPrecoTotal("Item total: $65.98");
buttonFinish.click();
driver.navigate().to("https://www.saucedemo.com/inventory.html");
}