1

Hi I'm very new to selenium and was wondering how it would be best to wait for an element to become enabled. I'm writing a test to download a report that requires a status to be set at "Complete" within a table before a download button appears. I have tried to use the isDisplayed() function but so far it fails. I also need the timer to be dynamic to keep checking until the report is completed before downloading.

public static boolean isElementVisible(WebElement element) throws Exception {

    return element.isDisplayed();
}


public void statusCheck() throws Exception {


    if(Utils.isElementVisible(status)){

        downloadButton.click();
    }
    else{
        wait(100000);
    }

}
DebanjanB
  • 118,661
  • 30
  • 168
  • 217

2 Answers2

1

You can use WaitDriver instead.

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.or(
    ExpectedConditions.visibilityOfElementLocated(By.id("id1")),
    ExpectedConditions.visibilityOfElementLocated(By.id("id2"))
));

https://www.selenium.dev/documentation/en/webdriver/waits/ https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#or-org.openqa.selenium.support.ui.ExpectedCondition

0

A bit of more information regarding the elements, if wait for an element and status to be set at "Complete" refers to the same element or different element would have helped us to construct a canonical answer.

In case they both refers to the same WebElement instead of validating with isDisplayed() you need to induce WebDriverWait for the elementToBeClickable​(WebElement element) and you can use the following Locator Strategy:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(element)).click();

Note: There is no such method as isElementVisible() for webelements.


In case they both refers to different WebElements you can induce WebDriverWait for both the elementToBeClickable​() using the and clause and you can use the following Locator Strategy:

new WebDriverWait(driver, 10).until(ExpectedConditions.and(
    ExpectedConditions.elementToBeClickable(By.cssSelector("elementAcss")),
    ExpectedConditions.elementToBeClickable(By.xpath("elementBxpath"))
));

You can find a relevant detailed discussion in How to wait for either of the two elements in the page using selenium xpath

DebanjanB
  • 118,661
  • 30
  • 168
  • 217