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();
//暂停执行1秒
Thread.sleep(1000);
long startaftersleep = System.currentTimeMillis();
System.out.println("Sleep time in ms = "+ startaftersleep - start);
}
}