Chrome specific functionality
默认情况下,Selenium 4与Chrome v75及更高版本兼容. 但是请注意Chrome浏览器的版本与chromedriver的主版本需要匹配.
Options
所有浏览器的通用功能请看这 Options page.
Chrome浏览器的特有功能可以在谷歌的页面找到: Capabilities & ChromeOptions
基于默认选项的Chrome浏览器会话看起来是这样:
ChromeOptions options = new ChromeOptions();
driver = new ChromeDriver(options);
options = ChromeOptions()
driver = webdriver.Chrome(options=options)
var options = new ChromeOptions();
var driver = new ChromeDriver(options);
options = Selenium::WebDriver::Options.chrome(args: ['--headless=new'])
const Options = new Chrome.Options();
let driver = await env
.builder()
.setChromeOptions(Options)
.build();
下面是一些不同功能的常见示例:
参数
args
参数是启动浏览器时输入的浏览器命令行参数.
常用的参数包括 --start-maximized
和 --headless=new
添加一个参数到选项中:
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
driver = new ChromeDriver(options);
chrome_options = ChromeOptions()
chrome_options.add_argument("--headless=new")
var options = new ChromeOptions();
options.AddArgument("--headless=new");
var driver = new ChromeDriver(options);
options = Selenium::WebDriver::Options.chrome(args: ['--headless=new'])
let driver = await env
.builder()
.setChromeOptions(options.addArguments('--headless=new'))
.build();
从指定位置启动浏览器
binary
参数接收一个使用浏览器的备用路径,通过这个参数你可以使用chromedriver 去驱动各种基于Chromium 内核的浏览器.
添加一个浏览器地址到选项中:
let driver = await env
.builder()
.setChromeOptions(options.setChromeBinaryPath(`Path to chrome binary`))
.build();
添加扩展程序
extensions
参数接受crx文件. As for unpacked directories,
please use the load-extension
argument instead, as mentioned in
this post.
添加一个扩展程序到选项中:
Coding Help
Check our contribution guidelines and code example formats if you’d like to help.
保持浏览器的打开状态
将 detach
参数设置为true将在驱动过程结束后保持浏览器的打开状态.
添加一个布尔值到选项中:
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("detach", true);
driver = new ChromeDriver(options);
chrome_options = ChromeOptions()
chrome_options.add_experimental_option("detach", True)
options = Selenium::WebDriver::Options.chrome(detach: true)
let driver = await env
.builder()
.setChromeOptions(options.detachDriver(true))
.build();
排除的参数
Chrome 添加了各种参数,如果你不希望添加某些参数,可以将其传入 excludeSwitches
.
一个常见的例子是重新打开弹出窗口阻止程序.
设置排除参数至选项中:
chrome_options = ChromeOptions()
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
options = Selenium::WebDriver::Options.chrome(exclude_switches: ['enable-automation'])
let driver = await env
.builder()
.setChromeOptions(options.excludeSwitches('enable-automation'))
.build();
Casting
你可以驱动 Chrome Cast 设备,包括共享选项卡
Coding Help
Check our contribution guidelines and code example formats if you’d like to help.
网络条件
您可以模拟各种网络条件.
The following examples are for local webdrivers. For remote webdrivers, please refer to the Remote WebDriver page.
Coding Help
Check our contribution guidelines and code example formats if you’d like to help.
Logs
Coding Help
Check our contribution guidelines and code example formats if you’d like to help.
Permissions
Coding Help
Check our contribution guidelines and code example formats if you’d like to help.
DevTools
See the Chrome DevTools section for more information about using Chrome DevTools