Edge 特定功能
Microsoft Edge 是使用 Chromium 实现的,最早支持的版本为 v79。与 Chrome 类似,edgedriver 的主要版本号必须与 Edge 浏览器的主要版本相匹配。
选项
所有浏览器通用的功能在选项页面上描述。
Chromium 特有的功能记录在 Google 的功能 & ChromeOptions页面上
使用基本定义的选项启动 Edge 会话如下所示
EdgeOptions options = new EdgeOptions();
driver = new EdgeDriver(options);
options = webdriver.EdgeOptions()
driver = webdriver.Edge(options=options)
var options = new EdgeOptions();
driver = new EdgeDriver(options);
options = Selenium::WebDriver::Options.edge
@driver = Selenium::WebDriver.for :edge, options: options
let options = new edge.Options();
driver = new Builder()
.forBrowser(Browser.EDGE)
.setEdgeOptions(options)
.build();
参数
args
参数用于在启动浏览器时使用的一系列命令行开关。有两个优秀的资源可以用来调查这些参数
常用的 args 包括 --start-maximized
、--headless=new
和 --user-data-dir=...
向选项添加参数
options.addArguments("--start-maximized");
options.add_argument("--start-maximized")
options.AddArgument("--start-maximized");
options.args << '--start-maximized'
.setEdgeOptions(options.addArguments('--headless=new'))
在指定位置启动浏览器
binary
参数接受要使用的浏览器备用位置的路径。使用此参数,您可以使用 chromedriver 来驱动各种基于 Chromium 的浏览器。
向选项添加浏览器位置
options.setBinary(getEdgeLocation());
options.binary_location = edge_bin
options.BinaryLocation = GetEdgeLocation();
options.binary = edge_location
添加扩展
extensions
参数接受 crx 文件。至于解压缩的目录,请使用 load-extension
参数,如此帖子中所述。
向选项添加扩展
options.addExtensions(extensionFilePath);
options.add_extension(extension_file_path)
options.AddExtension(extensionFilePath);
options.add_extension(extension_file_path)
.setEdgeOptions(options.addExtensions(['./test/resources/extensions/webextensions-selenium-example.crx']))
保持浏览器打开
将 detach
参数设置为 true 将在进程结束后保持浏览器打开,只要不向驱动程序发送退出命令。
注意:这已经是 Java 中的默认行为。
options.add_experimental_option("detach", True)
注意:这已经是 .NET 中的默认行为。
options.detach = true
.setEdgeOptions(options.detachDriver(true))
排除参数
MSEdgedriver 有几个它用来启动浏览器的默认参数。如果您不希望添加这些参数,请将它们传递到 excludeSwitches
中。一个常见的例子是重新打开弹出窗口拦截器。可以从Chromium 源代码中解析默认参数的完整列表
在选项上设置排除的参数
options.setExperimentalOption("excludeSwitches", List.of("disable-popup-blocking"));
options.add_experimental_option('excludeSwitches', ['disable-popup-blocking'])
options.AddExcludedArgument("disable-popup-blocking");
options.exclude_switches << 'disable-popup-blocking'
.setEdgeOptions(options.excludeSwitches('enable-automation'))
服务
有关创建默认 Service 对象以及设置驱动程序位置和端口的示例,请参见驱动服务页面。
日志输出
获取驱动程序日志有助于调试问题。Service 类允许您指定日志的去向。除非用户将其定向到某个位置,否则日志输出将被忽略。
文件输出
要更改日志输出以保存到特定文件
EdgeDriverService service = new EdgeDriverService.Builder().withLogFile(logLocation).build();
注意:Java 还允许通过系统属性设置文件输出
属性键:EdgeDriverService.EDGE_DRIVER_LOG_PROPERTY
属性值:表示日志文件路径的字符串
service = webdriver.EdgeService(log_output=log_path)
service.LogPath = GetLogLocation();
控制台输出
要更改日志输出以在控制台中显示为 STDOUT
EdgeDriverService service = new EdgeDriverService.Builder().withLogOutput(System.out).build();
注意:Java 还允许通过系统属性设置控制台输出;
属性键:EdgeDriverService.EDGE_DRIVER_LOG_PROPERTY
属性值:DriverService.LOG_STDOUT
或 DriverService.LOG_STDERR
日志级别
有 6 个可用的日志级别:ALL
、DEBUG
、INFO
、WARNING
、SEVERE
和 OFF
。请注意,--verbose
等效于 --log-level=ALL
,--silent
等效于 --log-level=OFF
,因此此示例仅是通用地设置日志级别
EdgeDriverService service =
new EdgeDriverService.Builder().withLoglevel(ChromiumDriverLogLevel.DEBUG).build();
注意:Java 还允许通过系统属性设置日志级别
属性键:EdgeDriverService.EDGE_DRIVER_LOG_LEVEL_PROPERTY
属性值:ChromiumDriverLogLevel
枚举的字符串表示形式
service = webdriver.EdgeService(service_args=['--log-level=DEBUG'], log_output=log_path)
日志文件功能
仅当记录到文件时,才有 2 个功能可用
- 追加日志
- 可读的时间戳
要使用它们,您还需要显式指定日志路径和日志级别。日志输出将由驱动程序而不是进程管理,因此可能会看到细微的差异。
EdgeDriverService service =
new EdgeDriverService.Builder().withAppendLog(true).withReadableTimestamp(true).build();
注意:Java 还允许通过系统属性切换这些功能
属性键:EdgeDriverService.EDGE_DRIVER_APPEND_LOG_PROPERTY
和 EdgeDriverService.EDGE_DRIVER_READABLE_TIMESTAMP
属性值:"true"
或 "false"
service = webdriver.EdgeService(service_args=['--append-log', '--readable-timestamp'], log_output=log_path)
service.args << '--append-log'
service.args << '--readable-timestamp'
禁用构建检查
Edge 浏览器和 msedgedriver 版本应匹配,如果不匹配,驱动程序将出错。如果禁用构建检查,则可以强制驱动程序与任何版本的 Edge 一起使用。请注意,这是一个不受支持的功能,并且不会调查错误。
EdgeDriverService service =
new EdgeDriverService.Builder().withBuildCheckDisabled(true).build();
注意:Java 还允许通过系统属性禁用构建检查
属性键:EdgeDriverService.EDGE_DRIVER_DISABLE_BUILD_CHECK
属性值:"true"
或 "false"
service = webdriver.EdgeService(service_args=['--disable-build-check'], log_output=log_path)
service.DisableBuildCheck = true;
Internet Explorer 模式
Microsoft Edge 可以在“Internet Explorer 兼容模式”下驱动,该模式结合使用 Internet Explorer 驱动程序类和 Microsoft Edge。有关更多详细信息,请阅读Internet Explorer 页面。
特殊功能
某些浏览器已实现了它们特有的附加功能。
投射
您可以使用 Edge 驱动 Chrome Cast 设备,包括共享选项卡
List<Map<String, String>> sinks = driver.getCastSinks();
if (!sinks.isEmpty()) {
String sinkName = sinks.get(0).get("name");
driver.startTabMirroring(sinkName);
driver.stopCasting(sinkName);
}
sinks = driver.get_sinks()
if sinks:
sink_name = sinks[0]['name']
driver.start_tab_mirroring(sink_name)
driver.stop_casting(sink_name)
sinks = @driver.cast_sinks
unless sinks.empty?
device_name = sinks.first['name']
@driver.start_cast_tab_mirroring(device_name)
expect { @driver.stop_casting(device_name) }.not_to raise_exception
网络条件
您可以模拟各种网络条件。
ChromiumNetworkConditions networkConditions = new ChromiumNetworkConditions();
networkConditions.setOffline(false);
networkConditions.setLatency(java.time.Duration.ofMillis(20)); // 20 ms of latency
networkConditions.setDownloadThroughput(2000 * 1024 / 8); // 2000 kbps
networkConditions.setUploadThroughput(2000 * 1024 / 8); // 2000 kbps
network_conditions = {
"offline": False,
"latency": 20, # 20 ms of latency
"download_throughput": 2000 * 1024 / 8, # 2000 kbps
"upload_throughput": 2000 * 1024 / 8, # 2000 kbps
}
driver.set_network_conditions(**network_conditions)
@driver.network_conditions = {offline: false, latency: 100, throughput: 200}
日志
LogEntries logs = driver.manage().logs().get(LogType.BROWSER);
logs = driver.get_log("browser")
logs = @driver.logs.get(:browser)
权限
driver.setPermission("camera", "denied");
driver.set_permissions('camera', 'denied')
@driver.add_permission('camera', 'denied')
@driver.add_permissions('clipboard-read' => 'denied', 'clipboard-write' => 'prompt')
DevTools
有关在 Edge 中使用 DevTools 的更多信息,请参见Chrome DevTools部分