【问题标题】:Selenium Webdriver AJAX - How to wait for the Request to CompleteSelenium Webdriver AJAX - 如何等待请求完成
【发布时间】:2013-07-20 00:07:30
【问题描述】:

我有一种情况无法继续进行:

我在页面上有两个按钮(两个按钮都在同一个框架中)。我将Iterator 用于Button 1。当我们单击Button 1 时,它会进行 AJAX 调用。我看到一秒钟的鼠标加载动画,然后产品被添加到购物车中。

说,如果我有 5 个相同类型的按钮并且想要单击所有 5 个按钮,当我使用迭代器单击 5 个按钮时,控件将跳出循环而不是单击第二个按钮。

Iterator<WebElement> button1 = T2.iterator();
while(button1.hasNext()) {
    button1.next().click(); 
    Thread.sleep(10000);
}

driver.findElement(By.id("button2 id")).click();

如果我使用Thread.sleep(),它工作正常。但我不想用它作为不正确的方法。

button2 id 也处于启用状态,我无法使用Wait for the element to be enabled。当执行到button1.next().click() 时,我立即进入下一行,而没有完成循环。如果我在button1.next().click() 之后打印一些文本,文本会打印 5 次。但是不知道为什么按钮没有被点击。

甚至尝试过隐式等待,但没有运气。

避免此类问题的最佳方法是什么?

【问题讨论】:

    标签: java selenium webdriver selenium-webdriver


    【解决方案1】:

    正确的方法是明确等待产品被添加到购物车。操作完成后页面上的某些内容会发生变化,对吗?所以等待这种变化发生!很明显,我不知道你的网页,但大致上是这样的:

    // earlier
    import static org.openqa.selenium.support.ui.ExpectedConditions.*;
    
    // also earlier, if you want, but you can move it to the loop
    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    Iterator<WebElement> button = T2.iterator();
    while(button.hasNext()) {
        // find the current number of products in the cart
        String numberOfProductsInCart = driver.findElement(By.id("cartItems")).getText();
        button.next().click();
        // wait for the number of items in cart to change
        wait.until(not(textToBePresentInElement(By.id("cartItems"), numberOfProductsInCart)));
    }
    

    【讨论】:

    猜你喜欢
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 2014-02-19
    • 2013-03-27
    • 1970-01-01
    相关资源
    最近更新 更多