小编典典

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

selenium

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

我的情况有些不同。在我的方案中,进度条出现时,所有元素都被禁用。我正在使用显式等待,但仍收到异常。

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

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

这是我的代码:

WebDriverWait myWaitVar = new WebDriverWait(driver,20);

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

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

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

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

我可以使其正常工作的唯一方法是强制其进入睡眠状态:

Thread.sleep(3000);

该脚本与睡眠正常工作。

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


阅读 700

收藏
2020-06-26

共1个答案

小编典典

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

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

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

希望它能工作… :)

2020-06-26