在Selenium Webdriver中,ExpectedCondition.elementToBeClickable不会等到进度条消失 [英] In Selenium Webdriver, ExpectedCondition.elementToBeClickable is not waiting until the progress bar disappears

查看:844
本文介绍了在Selenium Webdriver中,ExpectedCondition.elementToBeClickable不会等到进度条消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似于下面的问题:

ie如何等到进度条消失。

如何动态等待,直到进度条在Selenium Webdriver中完全加载?

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?

我的情况略有不同。此处出现进度条时,将禁用所有元素。所以我使用显式等待但仍然获得异常。


$
场景:
在注册页面中,在提供所有详细信息后,脚本点击创建帐户按钮。此时,如果输入的密码无效(仅使用无效密码进行验证),则会出现循环进度条并持续1或2秒,错误消息将显示在注册页面的顶部。现在我需要点击取消按钮并重复此过程。

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.

我使用WebDriverWait编写了相同的代码,如下所示:

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();

当我执行上面的代码时,每次都得到 NoSuchElementException 在最后一行。


我试过 ExpectedCondition.visibilityOfElement()但这里也得到 NoSuchElementException

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.

然后我尝试使用sleep方法而不是等待。

I then tried using sleep method instead of wait.

Thread.sleep(3000);

现在脚本工作正常。

Now the script is working fine.

我无法理解为什么 WebDriverWait 没等到进度条消失?

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

它已成功解析 elementToBeClickable()但仍然抛出异常,我们点击它。

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

推荐答案

ExpectedConditions.elementToBeClickable 如果condition为true,则返回元素表示如果元素出现在页面上并且可单击,则返回元素,无需再次找到此元素,只需省略下一行: -

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 : - 如果因其他元素而无法点击接收点击,您可以使用 JavascriptExecutor 执行点击,如下所示:

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 : - 从提供的异常看来,进度条仍然覆盖在 cancelRegister 按钮。所以最好的方法是先等待进度条的隐形,然后等待 cancelRegister 按钮的可见性,如下所示:

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...:)

这篇关于在Selenium Webdriver中,ExpectedCondition.elementToBeClickable不会等到进度条消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆