Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium Webdriver AJAX-如何等待请求完成_Java_Selenium_Webdriver_Selenium Webdriver - Fatal编程技术网

Java Selenium Webdriver AJAX-如何等待请求完成

Java Selenium Webdriver AJAX-如何等待请求完成,java,selenium,webdriver,selenium-webdriver,Java,Selenium,Webdriver,Selenium Webdriver,我有一个无法继续的场景: 我在页面上有两个按钮(两个按钮在同一个框架中)。我正在对按钮1使用迭代器。当我们单击按钮1时,它会发出一个AJAX调用。我看到一个鼠标加载动画一秒钟,然后产品被添加到购物车 比方说,如果我有5个相同类型的按钮&想要单击所有5个按钮,当我使用迭代器单击这5个按钮时,控件将退出循环,而不是单击第二个按钮 Iterator<WebElement> button1 = T2.iterator(); while(button1.hasNext()) { but

我有一个无法继续的场景:

我在页面上有两个按钮(两个按钮在同一个框架中)。我正在对
按钮1
使用
迭代器。当我们单击
按钮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();
Iterator按钮1=T2.Iterator();
while(button1.hasNext()){
按钮1.下一步().单击();
睡眠(10000);
}
driver.findElement(By.id(“button2id”))。单击();
如果我使用
线程.sleep()
,它工作正常。但我不想用它,因为它不是正确的方式

button2 id也处于启用状态,我无法使用。当执行到
按钮1.next().click()
时,它会立即转到下一行,而不会完成循环。如果我在
按钮1.next()之后打印一些文本,单击()
,文本将打印5次。但不知道为什么按钮没有点击

甚至尝试过含蓄的等待,但没有运气


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

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

// 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)));
}
//更早
导入静态org.openqa.selenium.support.ui.ExpectedConditions.*;
//如果需要,也可以在前面,但可以将其移动到循环中
WebDriverWait wait=新的WebDriverWait(驱动程序,10);
迭代器按钮=T2.Iterator();
while(button.hasNext()){
//查找购物车中当前的产品数量
String numberOfProductsInCart=driver.findElement(By.id(“cartItems”)).getText();
按钮。下一步()。单击();
//等待购物车中的项目数更改
wait.until(不是(texttobepresentElement(By.id(“cartItems”),numberOfProductsInCart));
}

@VenkateshLakshmanan@VenkateshLakshmanan您能将此答案标记为您接受的答案吗?(单击答案左侧的复选标记。)