如何让Selenium等待Ajax响应?
我们可以让Selenium等待Ajax响应。由于Ajax响应导致的页面加载时间的确定是一项困难的任务。这可以通过Selenium中的同步概念和等待方法来实现:
隐式等待 - 它允许WebDriver等待指定的时间,之后抛出异常。此等待适用于测试中的所有步骤。
语法
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
示例
使用隐式等待的代码实现。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class AjaxImplWt{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// wait for page load
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://tutorialspoint.com/index.htm");
// identify element
WebElement e=driver.findElement(By.className("gsc-input"));
e.sendKeys("Java");
driver.close();
}
}Ajax响应调用也可以使用显式等待来处理。此等待应用于特定元素。对某个元素的预期条件进行指定时间的等待。一旦等待时间过去,就会抛出异常。
示例
使用显式等待的代码实现。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AjaxWaitExplicit{
public static void main(String[] args)
throws InterruptedException{
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://tutorialspoint.com/index.htm");
// identify element
WebElement l=driver.findElement(By.xpath("//*[text()='Library']"));
l.click();
//explicit wait condition
WebDriverWait wt = new WebDriverWait(driver,5);
wt.until(ExpectedConditions.
invisibilityOfElementLocated(By.xpath("//*[@class='mui−btn']")));
driver.quit();
}
}我们还可以使用Selenium WebDriver添加自定义ExpectedConditions。当WebDriver提供的默认预期条件不足时,我们需要自定义ExpectedConditions。“until”方法是WebDriverWait类的一部分。
示例
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AjaxCustomExpCondition{
public static void main(String[] args)
throws InterruptedException{
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
driver.get("https://tutorialspoint.com/about/about_careers.htm");
// identify element
WebElement l=driver.findElement(By.linkText("Team"));
l.click();
//object of WebDriverWait class
WebDriverWait w = new WebDriverWait(driver,7);
//custom expected condition with until method
w.until(new ExpectedCondition <Boolean> (){
public Boolean apply(WebDriver driver) {
//identify paragraph
WebElement e= driver.findElement(By.tagName("p"));
if (e!= null){
//to check if paragraph is displayed and has text India
if (e.isDisplayed() && e.getText().contains("India")) {
System.out.println("Element found");
return true;
}
else {
System.out.println("Element not found");
return false;
}
}
return false;
}
});
driver.close();
}
}此外,JavaScript执行器可用于等待Ajax调用。executeScript方法用于运行JavaScript命令。等待直到jQuery.active命令返回0。
示例
使用JavaScript的代码实现。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
public class AjaxJavaScrptWt{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://tutorialspoint.com/index.htm");
//wait with JavaScript Executor
while (true){
if ((Boolean) ((JavascriptExecutor)driver).
.executeScript("return jQuery.active == 0")){
break;
}
Thread.sleep(500);
}
}
}
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP