Selenium 简明教程
Selenium WebDriver - Chrome Options
ChromeOptions 是 Selenium Webdriver 中的一个特定类,它有助于处理仅适用于 Chrome 驱动的选项。它有助于修改浏览器在 Chrome 上运行自动化测试时的设置和功能。ChromeOptions 类扩展了另一个名为 MutableCapabilities 的类。
ChromeOptions is a specific class in Selenium Webdriver which helps to handle options which are only applicable to the Chrome driver. It helps to modify the settings and capabilities of the browser while running an automated test on Chrome. The ChromeOptions class extends another class known as the MutableCapabilities class.
ChromeOptions 类从 Selenium 3.6 版本开始添加。默认情况下,Selenium Webdriver 以全新的浏览器配置文件启动,无任何预定义的设置和 cookie、历史记录等。
The ChromeOptions class was added from the Selenium 3.6 version onwards. Selenium Webdriver begins with a fresh browser profile without any predefined settings on cookies, history, and so on by default.
Add Chrome Extensions Using ChromeOptions
让我们举个例子,我们将在 Chrome IDE 扩展程序中打开 Chrome 浏览器。Chrome 扩展应该具有 .crx 文件。对于此示例,我们将获取 Selenium IDE Chrome 扩展的 .crx 文件并将其放置在测试项目的 Resources 文件夹中。
Let us take an example, where we would open the Chrome browser with the Selenium IDE extension. A Chrome extension should have a .crx file. For this example, we would get the .crx file for the Selenium IDE Chrome extension and place it under the Resources folder within the test project.
Example
package org.example;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.util.concurrent.TimeUnit;
public class ChromeOptnsExtension {
public static void main(String[] args) throws InterruptedException {
// object of ChromeOptions
ChromeOptions opt = new ChromeOptions();
// adding .crx extension
opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));
// Initiate the Webdriver
WebDriver driver = new ChromeDriver(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");
}
}
Process finished with exit code 0
Chrome 浏览器使用 Selenium IDE 扩展程序和信息栏 Chrome is being controlled by automated test software 打开。
Chrome browser opened with the Selenium IDE extension along with the information bar Chrome is being controlled by automated test software.
Disable Infobar Using ChromeOptions
在前面的示例中,我们使用文本 Chrome is being controlled by automated test software 获取了一个信息栏,但是我们可以使用 ChromeOptions 类禁用此信息栏。
In the previous example, we obtained an information bar with the text Chrome is being controlled by automated test software, however we can disable this information bar using the ChromeOptions class.
Example
package org.example;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
public class ChromeOptns {
public static void main(String[] args) throws InterruptedException {
// object of ChromeOptions
ChromeOptions opt = new ChromeOptions();
// adding .crx extensions
opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));
// disable information bar
opt.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
// Initiate the Webdriver
WebDriver driver = new ChromeDriver(opt);
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage with disabling information bar
driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");
}
}
Process finished with exit code 0
Chrome 浏览器使用 Selenium IDE 扩展程序且没有信息栏打开。
Chrome browser launched with the Selenium IDE extension without the information bar.
Open Maximized Browser Using ChromeOptions
在此示例中,我们将在最大化模式下的 Chrome 浏览器中打开并启动一个应用程序。
In this example, we would open and launch an application in the Chrome browser in the maximized mode.
Example
package org.example;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
public class ChromeOptnsMaximized {
public static void main(String[] args) throws InterruptedException {
// object of ChromeOptions
ChromeOptions opt = new ChromeOptions();
// adding .crx extensions
opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));
// disable information bar
opt.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
// open browser in maximized
opt.addArguments("--start-maximized");
// Initiate the Webdriver
WebDriver driver = new ChromeDriver(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");
// quitting browser
driver.quit();
}
}
Process finished with exit code 0
在上面的示例中,我们观察到 Chrome 浏览器是使用 Selenium IDE 扩展程序在最大化的浏览器中打开的,没有信息栏 explicit 。
In the above example, we observed that the Chrome browser was launched with the Selenium IDE extension without the information bar Chrome is being controlled by automated test software in a maximized browser.
SSL Certificates Using ChromeOptions
要处理 Chrome 中的 SSL 证书,将 ChromeOptions 类与 DesiredCapabilities 类结合使用。要获取 DesiredCapabilities 的功能以便与 ChromeOptions 配合使用,请使用 merge 方法。
To handle SSL certificates in Chrome, the ChromeOptions class along with the DesiredCapabilities class is used. To obtain the capabilities of the DesiredCapabilities to be available with the ChromeOptions, the merge method is used.
Example
package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;
public class SSLErrorInChrome {
public static void main(String[] args) throws InterruptedException {
// Desired Capabilities class to profile Chrome
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
// Chrome Options class
ChromeOptions opt = new ChromeOptions();
// merging browser capabilities with options
opt.merge(dc);
// Initiate the Webdriver with options
WebDriver driver = new ChromeDriver(opt);
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// launch application
driver.get("https://expired.badssl.com");
// obtain browser title
System.out.println("Browser title in Chrome: " + driver.getTitle());
// quit the browser
driver.quit();
}
}
Browser title in Chrome: Privacy error
Conclusion
这就总结了我们对 Selenium Webdriver Chrome Options 教程的全面讲解。我们从描述 ChromeOptions 类开始,并逐步了解了如何向 Chrome 浏览器添加扩展、如何禁用信息栏、如何最大化浏览器以及如何在 Selenium Webdriver 中借助 ChromeOptions 处理 SSL 证书错误。这让你对 Selenium Webdriver 中的 ChromeOptions 类有了深入的了解。明智的做法是继续练习你学到的知识,并探索与 Selenium 相关的内容,以加深你的理解并拓展你的视野。
This concludes our comprehensive take on the tutorial on Selenium Webdriver Chrome Options. We’ve started with describing a ChromeOptions class, and walked through examples of how to add extensions to Chrome browser, how to disable information bar, how to maximize the browser, and handle SSL certificates errors taking help of ChromeOptions along with Selenium Webdriver. This equips you with in-depth knowledge of the ChromeOptions 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.