讓Selenium暫停X秒。


我們可以利用同步的概念讓Selenium暫停X秒。有兩種型別的等待——**隱式等待**和**顯式等待**。除此之外,還有**Thread.sleep**方法,它可以使Selenium暫停一段時間。等待時間作為引數傳遞給該方法。

示例

使用Thread.sleep的程式碼實現。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ThreadWt{
   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");
      // identify element, enter text
      WebElement n=driver.findElement(By.className("gsc-input"));
      // wait of 200 ms applied
      Thread.sleep(200);
      n.sendKeys("Selenium");
      driver.quit();
   }
}

我們可以指定隱式等待。它將使驅動程式等待特定時間,直到元素可用。

語法

driver.manage().timeouts().implicitlyWait();

隱式等待是應用於頁面上每個元素的全域性等待,並且是動態的。

示例

使用隱式等待的程式碼實現。

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 ImplicitWt{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://tutorialspoint.tw/index.htm";
      driver.get(url);
      // wait of 5 seconds
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify element, enter text
      WebElement n=driver.findElement(By.className("gsc−input"));
      n.sendKeys("Selenium");
      driver.quit();
   }
}

顯式等待也被使用,它應用於頁面上的特定元素。它是一個與Expected Condition類一起工作的WebDriverWait。它也是動態的。

顯式等待的預期條件是:

  • titleContains

  • alertIsPresent

  • invisibilityOfElementLocated

  • invisibilityOfElementWithText

  • textToBePresentInElement

  • visibilityOfElementLocated

  • presenceOfAllElementsLocatedBy

  • visibilityOf

  • presenceOfElementLocated

  • elementToBeClickable

  • stalenessOf

示例

使用顯式等待的程式碼實現。

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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExpltWaits{
   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");
      // identify element and click()
      driver.findElement(By.xpath("//*[text()='Library']")).click();
      // expected condition - invisibility condition
      WebDriverWait wt = new WebDriverWait(driver,5);
      // invisibilityOfElementLocated condition
      wt.until(ExpectedConditions.
      invisibilityOfElementLocated(By.xpath("//*[@class='mui-btn']")));
      driver.close();
   }
}

更新於:2020年11月30日

2K+ 瀏覽量

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告