Selenium 简明教程

Selenium WebDriver - EdgeOptions

EdgeOptions 是 Selenium Webdriver 中的一个特定类,它有助于处理仅适用于 Edge 驱动程序的选项。在 Edge 上运行自动化测试时,它有助于修改浏览器的设置和功能。EdgeOptions 类扩展了另一个名为 MutableCapabilities 的类。

EdgeOptions is a specific class in Selenium Webdriver which helps to handle options which are only applicable to the Edge driver. It helps to modify the settings and capabilities of the browser while running an automated test on Edge. The EdgeOptions class extends another class known as the MutableCapabilities class.

EdgeOptions 类在最新版本的 Selenium 中可用。默认情况下,Selenium Webdriver 以没有关于 cookie、历史记录等任何预定义设置的新鲜浏览器配置文件开始。

The EdgeOptions class is available in the latest version of Selenium. Selenium Webdriver begins with a fresh browser profile without any predefined settings on cookies, history, and so on by default.

Add Edge Extensions Using EdgeOptions

使用 EdgeOptions 类使用 Selenium IDE 扩展启动 Edge 浏览器。Edge 扩展应具有 .crx 扩展名。我们将保留扩展的 .crx 文件并将其放置在项目中的 Resources 文件夹下。

Launch the Edge browser with a Selenium IDE extension using the EdgeOptions class. An Edge extension should have extension as .crx. We would keep the .crx file for an extension and place it under the Resources folder within the project.

selenium edge options 1

Example

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import java.io.File;
import java.util.concurrent.TimeUnit;

public class EdgeOptnsExtension {
   public static void main(String[] args) throws InterruptedException {

      // object of EdgeOptions
      EdgeOptions opt = new EdgeOptions();

      // adding .crx extension to project structure
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with Selenium IDE extension
      driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");
   }
}

它将显示以下输出 −

It will show the following output −

Process finished with exit code 0
selenium edge options 2

Edge 浏览器已连同信息栏 Microsoft Edge is being controlled by automated test software 一起使用 Selenium IDE 扩展启动。

The Edge browser got launched with the Selenium IDE extension along with the information bar Microsoft Edge is being controlled by automated test software.

Disable Pop-up Blockers Using EdgeOptions

让我们举个例子,我们将用禁用的弹出窗口阻止程序打开 Edge 浏览器。

Let us take an example, where we would open the Edge browser with disabled pop-up blocker.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class EdgeOptnsBlockPopUp {
   public static void main(String[] args) throws InterruptedException {

      // object of EdgeOptions
      EdgeOptions opt = new EdgeOptions();

      // adding .crx extension to project structure
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // disable pop-up blocker
      opt.setExperimentalOption("excludeSwitches", List.of("disable-popup-blocking"));

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with Selenium IDE extension
      driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");
   }
}

它将显示以下输出 −

It will show the following output −

Process finished with exit code 0

在上面的示例中,Edge 浏览器随 Selenium IDE 扩展一起启动且阻止了弹出窗口。

In the above example, the Edge browser was launched with the Selenium IDE extension and pop-up blocked.

Open Maximized Browser Using EdgeOptions

在这个示例中,我们将在最大化尺寸中打开和启动 Edge 浏览器。

In this example, we would open and launch the Edge browser in the maximized size.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class EdgeOptnsMaximized {
   public static void main(String[] args) throws InterruptedException {

      // object of EdgeOptions
      EdgeOptions opt = new EdgeOptions();

      // adding .crx extension
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // disable pop-up blocker
      opt.setExperimentalOption("excludeSwitches", List.of("disable-popup-blocking"));

      // open browser in maximized mode
      opt.addArguments("--start-maximized");

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver(opt);

      // adding implicit wait of 20 secs
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/slider.php");
   }
}

它将显示以下输出 −

It will show the following output −

Process finished with exit code 0

在上面的示例中,我们观察到 Edge 浏览器随 Selenium IDE 扩展、弹出窗口拦截器和最大化的浏览器一起启动。

In the above example, we observed that the Edge browser was launched with the Selenium IDE extension with the pop-up blocker and in a maximized browser.

Open in Headless Browser Using EdgeOptions

在这个示例中,我们将在 Edge 无界面模式中打开和启动一个应用程序。

In this example, we would open and launch an application in the Edge headless mode.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class EdgeOptnsHeadless {
   public static void main(String[] args) throws InterruptedException {

      // object of EdgeOptions
      EdgeOptions opt = new EdgeOptions();

      // adding .crx extension to project structure
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // disable pop-up blocker
      opt.setExperimentalOption("excludeSwitches", List.of("disable-popup-blocking"));

      // open in headless mode
      opt.addArguments("--headless=new");

      // Initiate the Webdriver
      WebDriver driver = new EdgeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/progress-bar.php");

      // getting page title
      System.out.println("Getting the page title in headless mode: " + driver.getTitle());

      // Quitting browser
      driver.quit();
   }
}

它将显示以下输出 −

It will show the following output −

Getting the page title: Selenium Practice - Student Registration Form

在上面的示例中,我们观察到 Edge 浏览器随 Selenium IDE 扩展、弹出窗口拦截器且在无界面模式下启动。我们还使用控制台 Getting the page title in headless mode: Selenium Practice - Progress Bar 中的该消息获得了浏览器标题。

In the above example, we observed that the Edge browser was launched with the Selenium IDE extension with the popup blocker and in a headless mode. We had also obtained the browser title with the message in the console - Getting the page title in headless mode: Selenium Practice - Progress Bar.

Conclusion

这结束了我们在教程中对 Selenium Webdriver Edge 选项的全面讲解。我们从描述 EdgeOptions 类开始,并逐步讲解了一些示例,介绍如何将扩展程序添加到 Edge 浏览器、如何阻止弹出窗口、如何最大化浏览器以及借助 Selenium Webdriver 在 EdgeOptions 的帮助下处理无界面 Edge 浏览器执行。它使你具备有关 Selenium Webdriver 中 EdgeOptions 类的深入知识。明智的做法是继续练习你所学的内容并探索其他与 Selenium 相关的知识来加深你的理解并拓宽你的视野。

This concludes our comprehensive take on the tutorial on Selenium Webdriver Edge Options. We’ve started with describing a EdgeOptions class, and walked through examples of how to add extensions to Edge browser, how to block pop-ups, how to maximize the browser, and handle headless Edge browser execution taking help of EdgeOptions along with Selenium Webdriver. This equips you with in-depth knowledge of the EdgeOptions class in Selenium Webdriver. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.