如何讓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.tw/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.tw/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.tw/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.tw/index.htm"); //wait with JavaScript Executor while (true){ if ((Boolean) ((JavascriptExecutor)driver). .executeScript("return jQuery.active == 0")){ break; } Thread.sleep(500); } } }
廣告