让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.com/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.com/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.com/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+ 浏览量

启动你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.