java – 在Selenium Webdriver中,ExpectedCondition.elementToBeClickable不会等到进度条消失
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 在Selenium Webdriver中,ExpectedCondition.elementToBeClickable不会等到进度条消失,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2738字,纯文字阅读大概需要4分钟。
内容图文
这个问题类似于下面的问题:
即如何等到进度条消失.
How to wait dynamically until the progress bar to load completely in Selenium Webdriver?
我有点不同.此处出现进度条时,将禁用所有元素.所以我使用显式等待,但仍然得到例外.
场景:
在注册页面中,在提供所有详细信息后,脚本单击“创建帐户”按钮.此时,如果输入的密码无效(仅使用无效密码验证),则会出现循环进度条并持续1或2秒,错误消息显示在“注册”页面的顶部.现在我需要点击“取消”按钮并重复该过程.
出现进度条时,将禁用整个页面.只有在进度条消失后,用户才能继续.
我使用WebDriverWait编写了相同的代码,如下所示:
WebDriverWait myWaitVar = new WebDriverWait(driver,20);
点击“创建帐户”按钮后,进度条出现并等待,直到出现取消按钮.
//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.
然后我尝试使用sleep方法而不是等待.
Thread.sleep(3000);
现在脚本运行正常.
我无法理解为什么WebDriverWait没有等到进度条消失?
它已经成功解析了elementToBeClickable(),但是当我们点击它时仍然抛出异常.
最佳答案:
如果condition为true,则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: – 如果由于其他元素接收而无法点击,您可以使用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);
编辑2: – 似乎从提供的异常,进度条仍然覆盖在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();
希望它有效……
内容总结
以上是互联网集市为您收集整理的java – 在Selenium Webdriver中,ExpectedCondition.elementToBeClickable不会等到进度条消失全部内容,希望文章能够帮你解决java – 在Selenium Webdriver中,ExpectedCondition.elementToBeClickable不会等到进度条消失所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。