Currently, I am using Selenium WebDriver for automating my script. In the script, I have implemented a wait.until condition to click on the delivery button on the HTML page below. The issue arises because even though Selenium can locate my element, the JavaScript reloads the specific element causing the delivery button to only become clickable post-reload. As a result, my Selenium script throws an error stating "stale element reference: element is not attached to the page document". How can I resolve this error?
WebElement deliverButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//button[@class='btn-to-cart nopricetohide btn btn-primary your-catalog-deliver btn-block btn-unpadded tocart-rounded'])[1]")));
deliverButton.click();
WebElement continueShoppingLink = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='btn btn-link full-width mb-10']")));
Screenshot: