让 Selenium 暂停 X 秒。

software testingautomation testingselenium web driver更新于 2026/2/7 20:22:17

我们可以通过同步的概念让 Selenium 暂停 X 秒。有两种等待类型:隐式 和 显式。此外,还有一个 Thread.sleep 方法,可以让 Selenium 暂停一段时间。等待时间作为参数传递给该方法。

示例

Code Implementation with 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://www.tutorialspoint.com/index.htm");
      // 识别元素,输入文本
      WebElement n=driver.findElement(By.className("gsc-input"));
      // 等待 200 毫秒
      Thread.sleep(200);
      n.sendKeys("Selenium");
      driver.quit();
   }
}

我们可以指定隐式等待。它应使驱动程序等待特定时间,直到元素可用。

语法

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

隐式等待是应用于页面上每个元素的全局等待,本质上是动态的。

示例

Code Implementation with implicit wait.

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://www.tutorialspoint.com/index.htm";
      driver.get(url);
      // 等待 5 秒
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // 识别元素,输入文本
      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://www.tutorialspoint.com/index.htm");
      // 识别元素并单击()
      driver.findElement(By.xpath("//*[text()='Library']")).click();
      // 预期条件 - 不可见条件
      WebDriverWait wt = new WebDriverWait(driver,5);
      // invisibilityOfElementLocated 条件
      wt.until(ExpectedConditions.
      invisibilityOfElementLocated(By.xpath("//*[@class='mui-btn']")));
      driver.close();
   }
}

相关文章