• Selenium Video Tutorials

Selenium WebDriver - 等待支持



Selenium Webdriver 可以与各种等待机制一起使用,例如显式隐式流畅等待,以实现同步并提供等待支持。等待主要应用于测试中,以处理当 Web 元素在 Selenium 测试预期它在页面或 DOM 中可用时不可用的情况。

整个页面加载完毕之前,通常会存在一些延迟时间,并且 Web 元素在网页上完全可用。Selenium Webdriver 中提供的等待机制有助于在元素以正确的状态出现在网页上/消失之前,阻止测试执行。

Selenium Webdriver 中可用的基本等待

Selenium Webdriver 中有多种等待机制可用。它们列在下面:

隐式等待

这是 Selenium 中可用的默认等待。它是一种适用于整个驱动程序会话的全局等待。默认等待时间为 0,这意味着如果找不到元素,则会立即抛出错误。

显式等待

它类似于添加到代码中的循环,该循环轮询网页以使特定场景在退出循环并移动到下一行代码之前变为 true。

流畅等待

这是驱动程序等待特定元素条件为 true 的最长时间。它还确定驱动程序在找到元素或抛出异常之前将进行验证的间隔(轮询间隔)。流畅等待是一种自定义显式等待,它提供选项以自动处理特定异常以及在发生异常时使用自定义消息。FluentWait 类用于向测试添加流畅等待。

语法

Wait wt = new FluentWait(driver) .withTimeout(20, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(ElementNotInteractableException.class); wt.until(ExpectedConditions.titleIs("Tutorialspoint"));

在上面的示例中,指定了超时和轮询间隔,这意味着驱动程序将等待 20 秒,并在超时时间内以 5 秒的间隔进行轮询,以满足Tutorialspoint浏览器标题条件。如果在该时间范围内未满足该条件,则将抛出异常,否则将执行下一步。

示例 1 - 显式等待

让我们以下面的图像为例,我们首先单击单击我按钮。

Selenium Wait Support 1

单击“单击我”后,我们将使用显式等待,并等待文本您已完成动态单击出现在网页上。

Selenium Wait Support 2

代码实现

package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import java.util.concurrent.TimeUnit; public class ExplicitsWait { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // launching a browser and open a URL driver.get("https://tutorialspoint.com/selenium/practice/buttons.php"); // identify button then click on it WebElement l = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[1]")); l.click(); // Identify text WebElement e = driver.findElement(By.xpath("//*[@id='welcomeDiv']")); // explicit wait to expected condition for presence of a text WebDriverWait wt = new WebDriverWait(driver, Duration.ofSeconds(2)); wt.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='welcomeDiv']"))); // get text System.out.println("Get text after clicking: " + e.getText()); // Quitting browser driver.quit(); } }

输出

Get text after clicking: You have done a dynamic click

Process finished with exit code 0

在上面的示例中,单击单击我按钮后获得的文本为您已完成动态单击

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例 2 - 流畅等待

让我们再举一个下面的页面的例子,我们首先点击颜色更改按钮。

Selenium Wait Support 3

单击颜色更改后,我们将使用流畅等待,并等待按钮5 秒后可见出现在网页上。

Selenium Wait Support 4

代码实现

package org.example; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; import java.time.Duration; public class Fluentwts { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // launching a browser and open a URL driver.get("https://tutorialspoint.com/selenium/practice/dynamic-prop.php"); // identify button then click WebElement l = driver.findElement(By.xpath("//*[@id='colorChange']")); l.click(); // fluent wait of 6 secs till other button appears Wait<WebDriver> w = new FluentWait<WebDriver>(driver) .withTimeout(Duration.ofSeconds(20)) .pollingEvery(Duration.ofSeconds(6)) .ignoring(NoSuchElementException.class); WebElement m = w.until(ExpectedConditions.visibilityOfElementLocated (By.xpath("//*[@id='visibleAfter']"))); // checking button presence System.out.println("Button appeared: " + m.isDisplayed()); // Quitting browser driver.quit(); } }

输出

Button appeared: true

Process finished with exit code 0

在上面的示例中,我们观察到单击颜色更改按钮后获得的按钮为5 秒后可见

结论

本教程全面介绍了 Selenium Webdriver 等待支持。我们首先介绍了 Selenium Webdriver 中可用的基本等待,并通过示例说明了 Selenium Webdriver 中的显式和流畅等待。这使您能够深入了解 Selenium Webdriver 等待支持。明智的做法是继续练习您所学的内容,并探索与 Selenium 相关的其他内容,以加深您的理解并拓宽视野。

广告