Reputation: 23
I want selenium to wait for 3 minutes then refresh the page until an element (Download button) is found.I have tried with below code but it's not working.
Note: I am uploading a zip file & it's sometimes taking few seconds to upload & sometimes taking 3 minutes.When the file is uploaded within few seconds, the code below is working fine. It's not working when zip file is taking longer time to upload.
Can anyone help me in this regard.
boolean displayed = false;
do {
try {
displayed = driver1.findElement(By.xpath("//*[@id=\"app\"]/main/div[1]/div/div/div/div[2]/table/tbody/tr[1]/td[4]/a")).isDisplayed();
} catch (NoSuchElementException e) {
System.out.println(e);
driver1.navigate().refresh();
}
} while (!displayed);
driver1.findElement(By.xpath("//*[@id=\"app\"]/main/div[1]/div/div/div/div[2]/table/tbody/tr[1]/td[4]/a")).click();
Upvotes: 1
Views: 4821
Reputation: 386
You mentioned, like "Until an element(Download button) is found".. so condition needs to meet here... hence we can use one of the wait functionalities provided by selenium
We have three types of wait in selenium, 1) Implicit wait 2) Explicit Wait, and 3) Fluent Wait.
1) Using implicit wait, code can be:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Usage: If we want WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
Not a good practice though, I personally not recommend
and
2) Using explicit wait, code can be:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("download_button_id")));
Usage: An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. If element takes a long time to load. Also, used to check property of an element (presence, clickability. etc).
3) Using Fluent wait, code can be:
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
Usage: Fluent wait is another type of Explicit wait and you can define polling and ignore the exception to continue with script execution in case element is not found. When you try to test the presence of an element that may appear after every x seconds/minutes (Just an example, this is my guess of where such a thing can be used)
Pick the suitable one, in your case, fluent wait may be apt
Upvotes: 1
Reputation: 4507
Instead of using .isDisplayed() method, you can the size of the element list and if the size is greater than 0 then you click on the element.
For example:
boolean displayed = false;
List<WebElement> element = driver.findElements(By.xpath("//*[@id=\"app\"]/main/div[1]/div/div/div/div[2]/table/tbody/tr[1]/td[4]/a"));
while (!displayed) {
if (element.size() > 0) {
// Element is found so set the boolean as true
displayed = true;
// Click on the element
element.get(0).click();
} else {
// Adding a static sleep of 10 seconds, this else condition can be removed also
Thread.sleep(10000);
}
}
Upvotes: 1