使用 Selenium 2 WebDriver 等待 Ajax 调用完成。
我们可以使用 Selenium webdriver 等待 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 添加自定义**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** 方法用于在 Selenium 中运行 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