Abdul Rahman Abdul Rahman - 4 months ago 70
Java Question

In Selenium Webdriver, ExpectedCondition.elementToBeClickable is not waiting until the progress bar disappears

This question is similar to the one below:

i.e. How to wait till Progress bar disappears.

How to wait dynamically until the progress bar to load completely in Selenium Webdriver?

Mine is bit different. Here when the progress bar appears, all the elements are disabled. So am using explicit wait but still getting the Exception.



Scenario:
In the Signup Page, , after providing all the details, the script clicks on "Create Account" button. At this instant, a circular progress bar appears and it persists for 1 or 2 seconds, if the password entered is invalid (am validating with invalid passwords only), error message is displayed at the top of the Signup page. Now I need to click on the "Cancel" button and repeat the process.



When the Progress bar appears, the whole page will be disabled. Only after the disappearance of progress bar, user will be able to continue.



I have written code for the same using WebDriverWait as below:



WebDriverWait myWaitVar = new WebDriverWait(driver,20);


After clicking on "Create Account" button, the progress bar comes and am waiting till cancel button appears.

//Click on Create Account btn:
driver.findElement(By.id("createAccount")).click();

//Wait till "Cancel" button is showing up. At cases, it may take some time.
myWaitVar.until(ExpectedConditions.elementToBeClickable (By.id("cancelRegister")));

//click on Cancel btn:
driver.findElement(By.id("cancelRegister")).click();


When I execute the above code, everytime am getting NoSuchElementException at the last line.


I tried with ExpectedCondition.visibilityOfElement() but here also am getting NoSuchElementException.

I then tried using sleep method instead of wait.

Thread.sleep(3000);


Now the script is working fine.



I couldn't get why WebDriverWait didn't wait till the progress bar disappears?



It has successfully parsed the elementToBeClickable() but still throwing exception, when we click on it.

Answer

ExpectedConditions.elementToBeClickable returns element if condition will be true means it returns element if element appears on the page and clickable, No need to find this element again, just omit last line as below :-

//Click on Create Account btn:
driver.findElement(By.id("createAccount")).click();

//Wait till "Cancel" button is showing up. At cases, it may take some time.
WebElement el = myWaitVar.until(ExpectedConditions.elementToBeClickable(By.id("cancelRegister")));
el.click();

Edited1 :- If you're unable to click due to other element receive click you can use JavascriptExecutor to perform click as below :

//Click on Create Account btn:
driver.findElement(By.id("createAccount")).click();

//Wait till "Cancel" button is showing up. At cases, it may take some time.
WebElement el = myWaitVar.until(ExpectedConditions.elementToBeClickable(By.id("cancelRegister")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el); 

Edited2 :- It seems from provided exception, progress bar still overlay on cancelRegister button. So best way is to wait for invisibility of progress bar first then wait for visibility of cancelRegister button as below :

//Click on Create Account btn:
driver.findElement(By.id("createAccount")).click();

//Now wait for invisibility of progress bar first 
myWaitVar.until(ExpectedConditions.invisibilityOfElementLocated(By.id("page_loader")));

//Now wait till "Cancel" button is showing up. At cases, it may take some time.
WebElement el = myWaitVar.until(ExpectedConditions.elementToBeClickable(By.id("cancelRegister")));
el.click();

Hope it works...:)

Comments