Selenium 中有哪些不同类型的等待可用?


Selenium 中的不同类型等待如下所示 −

  • 隐式等待

    这是 Selenium 中一种动态等待,语法如下 −

    driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
  • 显式等待

    这是 Selenium 中一种动态等待,语法如下 −

    WebDriverWait w = new WebDriverWait(driver,);
    w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("<<xpath expression>>")));
  • 流畅等待

    这是 Selenium 中一种动态等待,语法如下 −

    Wait<WebDriver> w = new
    FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofSeconds(3)).ignoring(NoSuchElementException.class);
  • 静态等待

用于暂停一段时间执行。

范例

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
public class ThreadWait {
   public static void main(String[] args) throws InterruptedException {
      long start = System.currentTimeMillis();
      // pause the execution 1 seconds
      Thread.sleep(1000);
      long startaftersleep = System.currentTimeMillis();
      System.out.println("Sleep time in ms = "+ startaftersleep - start);
   }
}

更新于:10-6-2020

686 次浏览

开启你的 职业

完成课程后获得认证

开始
广告