Java 在 Selenium Webdriver 中,ExpectedCondition.elementToBeClickable 不会等到进度条消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38673465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:30:45  来源:igfitidea点击:

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

javaseleniumselenium-webdriver

提问by Ahamed Abdul Rahman

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?

这个问题类似于下面的问题:
即如何等到进度条消失。
如何动态等待进度条在 Selenium Webdriver 中完全加载?

My situation is a bit different. In my scenario, all the elements are disabled when the progress bar appears. I am using explicit wait but still getting the exception.

Scenario:After providing all the details in the Signup Page the script clicks on the "Create Account" button. At this point a circular progress bar appears and it persists for 1 or 2 seconds. If the password entered is invalid the error message is displayed at the top of the Signup page. I now need to click on the "Cancel" button and repeat the process.

When the progress bar appears the whole page is disabled. The user will be able to continue only after the disappearance of the progress bar.

Here's my code:

WebDriverWait myWaitVar = new WebDriverWait(driver,20);

我的情况有点不同。在我的场景中,当进度条出现时,所有元素都被禁用。我正在使用显式等待,但仍然出现异常。

场景:在注册页面中提供所有详细信息后,脚本单击“创建帐户”按钮。此时会出现一个圆形进度条,并持续 1 或 2 秒。如果输入的密码无效,注册页面顶部会显示错误消息。我现在需要单击“取消”按钮并重复该过程。

当进度条出现时,整个页面都被禁用。只有在进度条消失后,用户才能继续。

这是我的代码:

WebDriverWait myWaitVar = new WebDriverWait(driver,20);

After clicking on the "Create Account" button the progress bar is displayed. The code should now wait until the "Cancel" button appears.

单击“创建帐户”按钮后,将显示进度条。代码现在应该等到“取消”按钮出现。

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

//Wait till the "Cancel" button shows up -- this may take some time.
myWaitVar.until(ExpectedConditions.elementToBeClickable (By.id("cancelRegister")));

//Click on the "Cancel" button.
driver.findElement(By.id("cancelRegister")).click();

When I execute the above code I always get NoSuchElementExceptionat the last line.

I tried with ExpectedCondition.visibilityOfElement()but this also produces NoSuchElementException.

当我执行上面的代码时,我总是NoSuchElementException在最后一行。

我尝试过,ExpectedCondition.visibilityOfElement()但这也会产生NoSuchElementException.

The only way I can get it to work is by forcing it to sleep:

我可以让它工作的唯一方法是强迫它进入睡眠状态:

Thread.sleep(3000);

The script works fine with the sleep.

该脚本在睡眠时运行良好。

Why doesn't WebDriverWaitwait until the progress bar disappears? The code successfully parses the elementToBeClickable()but it always throws an exception when the "Cancel" button is clicked.

为什么不WebDriverWait等到进度条消失?该代码成功解析了 ,elementToBeClickable()但在单击“取消”按钮时总是会引发异常。

采纳答案by Saurabh Gaur

ExpectedConditions.elementToBeClickablereturns 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 :-

ExpectedConditions.elementToBeClickable如果条件为真则返回元素意味着如果元素出现在页面上并且可点击,则返回元素,无需再次找到此元素,只需省略最后一行,如下所示:-

//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 JavascriptExecutorto perform click as below :

Edited1:- 如果由于其他元素接收点击而无法点击,您可以使用JavascriptExecutor如下执行点击:

//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 cancelRegisterbutton. So best way is to wait for invisibility of progress bar first then wait for visibility of cancelRegisterbutton as below :

Edited2:- 从提供的异常来看,进度条仍然覆盖在cancelRegister按钮上。所以最好的方法是先等待进度条不可见,然后等待cancelRegister按钮可见,如下所示:

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

希望它有效...:)

回答by Priyanshu Shekhar

You can have a wait there to make sure that progress bar disappears.

您可以在那里等待以确保进度条消失。

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.findElements(By.id("progressbar")).size() == 0);
 }
});