2016-07-30 51 views
4

此问题与以下类似:
即如何等待进度条消失。
How to wait dynamically until the progress bar to load completely in Selenium Webdriver?在Selenium Webdriver中,ExpectedCondition.elementToBeClickable并未等到进度条消失

煤矿有点不同。这里当进度条出现时,所有元素都被禁用。所以我使用明确的等待,但仍然得到例外。

场景: 在注册页面中,在提供所有细节后,脚本点击“创建帐户”按钮。此时,会出现一个圆形进度条,如果输入的密码无效(仅用无效密码进行验证),它会持续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

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

Thread.sleep(3000); 

现在脚本工作正常。

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

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

回答

3

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

希望工程... :)

+0

我试过这个,@Saurabh。它引发以下异常: org.openqa.selenium.WebDriverException:元素在点(575,446)处不可单击。其他元素将收到点击:

命令持续时间或超时:83毫秒 –

+0

@AbdulRahman意味着您现在可以找到元素。由于其他元素叠加造成的点击有问题。因此,在这种情况下,您可以使用'JavascriptExecutor'点击元素..尝试编辑答案..希望它有帮助... :) –

+0

@AbdulRahman作为例外明确说明您的进度条仍然会出现接收点击的情况,所以最好的方法是先等待进度条的隐身,然后等待取消注册按钮的可见性。看到第二个更新的答案.. :) –

2

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

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); 
} 
}); 
+0

是否适用方法返回WebElement OBJ或布尔?我认为它的布尔值。感谢您的评论。但我的目标是要知道为什么WebDriverWait不起作用? –