【问题标题】:In Selenium Webdriver, ExpectedCondition.elementToBeClickable is not waiting until the progress bar disappears在 Selenium Webdriver 中,ExpectedCondition.elementToBeClickable 不会等到进度条消失
【发布时间】:2016-12-05 00:51:19
【问题描述】:

这个问题与下面的问题类似:
即如何等到进度条消失。
How to wait dynamically until the progress bar to load completely in Selenium Webdriver?

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

场景: 在注册页面中提供所有详细信息后,脚本单击“创建帐户”按钮。此时会出现一个圆形进度条,并持续 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(),但在单击“取消”按钮时总是抛出异常。

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

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

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

    希望它有效...:)

    【讨论】:

    • 我试过了,@Saurabh。它会引发以下异常:org.openqa.selenium.WebDriverException: Element is not clickable at point (575, 446)。其他元素会收到点击:
      命令持续时间或超时:83 毫秒
    • @AbdulRahman 表示现在您可以找到元素了。由于其他元素的覆盖,点击存在问题。所以在这种情况下,您可以使用JavascriptExecutor..尝试编辑的答案..希望它有所帮助...:)
    • @AbdulRahman 提供的异常清楚地说您的进度条仍然出现,收到点击所以最好的方法是先等待进度条不可见,然后等待取消注册按钮的可见性。查看第二个更新的答案..:)
    • 第二次编辑对我有用,@Saurabh。现在我明白了这个问题。接受了你的回答。感谢您的回复。
    【解决方案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);
     }
    });
    

    【讨论】:

    • apply 方法返回 WebElement obj 还是布尔值?我认为它的布尔值。感谢您的评论。但我的目标是知道为什么 WebDriverWait 不起作用?
    猜你喜欢
    • 2017-04-07
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2013-11-24
    • 2020-01-12
    • 1970-01-01
    相关资源
    最近更新 更多