0

I have an application where I have to wait for a button to be visible on the page and click on it. The button is the one that has id="next-arrow-button", but on the parent div it has id="recorder-0"

enter image description here

After pressing on this button, the button won't be displayed anymore for a couple of seconds.

The button is displayed again like this: enter image description here

You can see that it has the same id ( id="next-arrow-button"), but on the parent div it has another id ( id="recorder-1" , before it was recoder-0)

I wait for the element to be visible and I try to click on it, but I got this error:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

My code is a bit messy, but I want to share with you a version of it:

 WebElement firstRecording = new WebDriverWait(driver, Constant.TIMEOUT_SECOND).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#recorder-0 #next-arrow-button")));
        firstRecording.click();


        new WebDriverWait(driver, Constant.TIMEOUT_SECOND).until(ExpectedConditions.stalenessOf(firstRecording));



        WebElement secondRecording = new WebDriverWait(driver, Constant.TIMEOUT_SECOND).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#recorder-1 #next-arrow-button")));
        secondRecording.click();

I tried to remove the line:

new WebDriverWait(driver, Constant.TIMEOUT_SECOND).until(ExpectedConditions.stalenessOf(firstRecording));

But I get the same error.

I am junior and I am sorry if I am doing a stupid mistake, but I really need you help. Thank you :)

Mihai C
  • 31
  • 4

1 Answers1

1

I managed to find a solution here. Just forget about the wait from selenium and use Java code:

 public void customWaitAndClick(WebDriver driver, String CSSelement) {
        boolean flag = true;
        long currentTime = System.currentTimeMillis();
        long end = currentTime + 60000L;

        while (!flag || System.currentTimeMillis() < end) {
            try {
                driver.findElement(By.cssSelector(CSSelement)).click();
                break;
            } catch (Exception e) {
                flag = false;
            }
        }
    }
Mihai C
  • 31
  • 4