selenium-驱动特定的功能


title: “驱动特定的功能”
weight: 2

Firefox

使用FirefoxOptions定义功能

FirefoxOptions 是为Firefox浏览器定义功能(Capabilities)的新方法,通常应优先使用预期功能(DesiredCapabilities).

{{< code-tab >}}
{{< code-panel language=“java” >}}
FirefoxOptions options = new FirefoxOptions();
options.addPreference(“network.proxy.type”, 0);
driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
var options = new FirefoxOptions();
options.Proxy.Kind = ProxyKind.Direct;
var driver = new FirefoxDriver(options);
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
require ‘selenium-webdriver’
opts = Selenium::WebDriver::Firefox::Options.new(args: [’-headless’])
driver = Selenium::WebDriver.for(:firefox, options: opts)
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const { Builder } = require(“selenium-webdriver”);
const firefox = require(‘selenium-webdriver/firefox’);

const options = new firefox.Options();
options.headless();
const driver = new Builder()
.forBrowser(‘firefox’)
.setFirefoxOptions(options)
.build();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
val options = new FirefoxOptions()
options.addPreference(“network.proxy.type”, 0)
driver = RemoteWebDriver(options)
{{< / code-panel >}}
{{< / code-tab >}}

设置自定义配置文件

可以为Firefox创建自定义配置文件, 如下所示.

{{< code-tab >}}
{{< code-panel language=“java” >}}
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
options=Options()
firefox_profile = FirefoxProfile()
firefox_profile.set_preference(“javascript.enabled”, False)
options.profile = firefox_profile
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
var options = new FirefoxOptions();
var profile = new FirefoxProfile();
options.Profile = profile;
var driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
profile = Selenium::WebDriver::Firefox::Profile.new
profile[‘browser.download.dir’] = “/tmp/webdriver-downloads”
options = Selenium::WebDriver::Firefox::Options.new(profile: profile)
driver = Selenium::WebDriver.for :firefox, options: options
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const { Builder } = require(“selenium-webdriver”);
const firefox = require(‘selenium-webdriver/firefox’);

const options = new firefox.Options();
let profile = ‘/path to custom profile’;
options.setProfile(profile);
const driver = new Builder()
.forBrowser(‘firefox’)
.setFirefoxOptions(options)
.build();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
val options = FirefoxOptions()
options.profile = FirefoxProfile()
driver = RemoteWebDriver(options)
{{< / code-panel >}}
{{< / code-tab >}}

Internet Explorer

fileUploadDialogTimeout

在某些环境中, 当打开文件上传对话框时, Internet Explorer可能会超时. IEDriver的默认超时为1000毫秒, 但您可以使用fileUploadDialogTimeout功能来增加超时时间.

{{< code-tab >}}
{{< code-panel language=“java” >}}
InternetExplorerOptions options = new InternetExplorerOptions();
options.waitForUploadDialogUpTo(Duration.ofSeconds(2));
WebDriver driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

options = webdriver.IeOptions()
options.file_upload_dialog_timeout = 2000
driver = webdriver.Ie(options=options)

Navigate to url

driver.get(“http://www.google.com”)

driver.quit()
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
var options = new InternetExplorerOptions();
options.FileUploadDialogTimeout = TimeSpan.FromMilliseconds(2000);
var driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
options = Selenium::WebDriver::IE::Options.new
options.file_upload_dialog_timeout = 2000
driver = Selenium::WebDriver.for(:ie, options: options)
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const ie = require(‘selenium-webdriver/ie’);
let options = new ie.Options().fileUploadDialogTimeout(2000);
let driver = await Builder()
.setIeOptions(options)
.build();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
val options = InternetExplorerOptions()
options.waitForUploadDialogUpTo(Duration.ofSeconds(2))
val driver = RemoteWebDriver(options)
{{< / code-panel >}}
{{< / code-tab >}}

ensureCleanSession

设置为 true时,
此功能将清除InternetExplorer所有正在运行实例的
缓存, 浏览器历史记录和Cookies
(包括手动启动或由驱动程序启动的实例) .
默认情况下,此设置为 false.

使用此功能将导致启动浏览器时性能下降,
因为驱动程序将等待直到缓存清除后再启动IE浏览器.

此功能接受一个布尔值作为参数.

{{< code-tab >}}
{{< code-panel language=“java” >}}
InternetExplorerOptions options = new InternetExplorerOptions();
options.destructivelyEnsureCleanSession();
WebDriver driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

options = webdriver.IeOptions()
options.ensure_clean_session = True
driver = webdriver.Ie(options=options)

Navigate to url

driver.get(“http://www.google.com”)

driver.quit()
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
var options = new InternetExplorerOptions();
options.EnsureCleanSession = true;
var driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
options = Selenium::WebDriver::IE::Options.new
options.ensure_clean_session = true
driver = Selenium::WebDriver.for(:ie, options: options)
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const ie = require(‘selenium-webdriver/ie’);
let options = new ie.Options().ensureCleanSession(true);
let driver = await Builder()
.setIeOptions(options)
.build();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
val options = InternetExplorerOptions()
options.destructivelyEnsureCleanSession()
val driver = RemoteWebDriver(options)
{{< / code-panel >}}
{{< / code-tab >}}

ignoreZoomSetting

InternetExplorer驱动程序期望浏览器的缩放级别为100%,
否则驱动程序将可能抛出异常.
通过将 ignoreZoomSetting 设置为 true,
可以禁用此默认行为.

此功能接受一个布尔值作为参数.

{{< code-tab >}}
{{< code-panel language=“java” >}}
InternetExplorerOptions options = new InternetExplorerOptions();
options.ignoreZoomSettings();
WebDriver driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

options = webdriver.IeOptions()
options.ignore_zoom_level = True
driver = webdriver.Ie(options=options)

Navigate to url

driver.get(“http://www.google.com”)

driver.quit()
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
var options = new InternetExplorerOptions();
options.IgnoreZoomLevel = true;
var driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
options = Selenium::WebDriver::IE::Options.new
options.ignore_zoom_level = true
driver = Selenium::WebDriver.for(:ie, options: options)
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const ie = require(‘selenium-webdriver/ie’);
let options = new ie.Options().ignoreZoomSetting(true);
let driver = await Builder()
.setIeOptions(options)
.build();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
val options = InternetExplorerOptions()
options.ignoreZoomSettings()
val driver = RemoteWebDriver(options)
{{< / code-panel >}}
{{< / code-tab >}}

ignoreProtectedModeSettings

启动新的IE会话时是否跳过 保护模式 检查.

如果未设置,
并且所有区域的 保护模式 设置都不同,
则驱动程序将可能引发异常.

如果将功能设置为 true,
则测试可能会变得不稳定, 无响应, 或者浏览器可能会挂起.
但是, 到目前为止,
这仍然是第二好的选择,
并且第一选择应该 始终 是手动实际设置每个区域的保护模式设置.
如果用户正在使用此属性,
则只会给予 “尽力而为” 的支持.

此功能接受一个布尔值作为参数.

{{< code-tab >}}
{{< code-panel language=“java” >}}
InternetExplorerOptions options = new InternetExplorerOptions();
options.introduceFlakinessByIgnoringSecurityDomains();
WebDriver driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

options = webdriver.IeOptions()
options.ignore_protected_mode_settings = True
driver = webdriver.Ie(options=options)

Navigate to url

driver.get(“http://www.google.com”)

driver.quit()
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
var options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
var driver = new RemoteWebDriver(options);
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
options = Selenium::WebDriver::IE::Options.new
options.ignore_protected_mode_settings = true
driver = Selenium::WebDriver.for(:ie, options: options)
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const ie = require(‘selenium-webdriver/ie’);
let options = new ie.Options().introduceFlakinessByIgnoringProtectedModeSettings(true);
let driver = await Builder()
.setIeOptions(options)
.build();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
val options = InternetExplorerOptions()
options.introduceFlakinessByIgnoringSecurityDomains()
val driver = RemoteWebDriver(options)
{{< / code-panel >}}
{{< / code-tab >}}

silent

设置为 true时,
此功能将禁止IEDriverServer的诊断输出.

此功能接受一个布尔值作为参数.

{{< code-tab >}}
{{< code-panel language=“java” >}}
InternetExplorerOptions options = new InternetExplorerOptions();
options.setCapability(“silent”, true);
WebDriver driver = new InternetExplorerDriver(options);
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

options = webdriver.IeOptions()
options.set_capability(“silent”, True)
driver = webdriver.Ie(options=options)

Navigate to url

driver.get(“http://www.google.com”)

driver.quit()
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
InternetExplorerOptions options = new InternetExplorerOptions();
options.AddAdditionalInternetExplorerOption(“silent”, true);
IWebDriver driver = new InternetExplorerDriver(options);
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}

Please raise a PR to add code sample

{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const {Builder,By, Capabilities} = require(‘selenium-webdriver’);
let caps = Capabilities.ie();
caps.set(‘silent’, true);

(async function example() {
let driver = await new Builder()
.forBrowser(‘internet explorer’)
.withCapabilities(caps)
.build();
try {
await driver.get(‘http://www.google.com/ncr’);
}
finally {
await driver.quit();
}
})();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
import org.openqa.selenium.Capabilities
import org.openqa.selenium.ie.InternetExplorerDriver
import org.openqa.selenium.ie.InternetExplorerOptions

fun main() {
val options = InternetExplorerOptions()
options.setCapability(“silent”, true)
val driver = InternetExplorerDriver(options)
try {
driver.get(“https://google.com/ncr”)
val caps = driver.getCapabilities()
println(caps)
} finally {
driver.quit()
}
}
{{< / code-panel >}}
{{< / code-tab >}}

IE 命令行选项

Internet Explorer包含几个命令行选项,
使您可以进行故障排除和配置浏览器.

下面介绍了一些受支持的命令行选项

  • -private : 用于在私有浏览模式下启动IE.
    这适用于IE 8和更高版本.

  • -k : 在kiosk模式下启动Internet Explorer.
    浏览器在一个最大化的窗口中打开,
    该窗口不显示地址栏, 导航按钮或状态栏.

  • -extoff : 在无附加模式下启动IE.
    此选项专门用于解决浏览器加载项问题.
    在IE 7和更高版本中均可使用.

注意: forceCreateProcessApi
应该启用命令行参数才能正常工作.

{{< code-tab >}}
{{< code-panel language=“java” >}}
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;

public class ieTest {
public static void main(String args) {
InternetExplorerOptions options = new InternetExplorerOptions();
options.useCreateProcessApiToLaunchIe();
options.addCommandSwitches("-k");
InternetExplorerDriver driver = new InternetExplorerDriver(options);
try {
driver.get(“https://google.com/ncr”);
Capabilities caps = driver.getCapabilities();
System.out.println(caps);
} finally {
driver.quit();
}
}
}
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

options = webdriver.IeOptions()
options.add_argument(’-private’)
options.force_create_process_api = True
driver = webdriver.Ie(options=options)

Navigate to url

driver.get(“http://www.google.com”)

driver.quit()
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;

namespace ieTest {
class Program {
static void Main(string args) {
InternetExplorerOptions options = new InternetExplorerOptions();
options.ForceCreateProcessApi = true;
options.BrowserCommandLineArguments = “-k”;
IWebDriver driver = new InternetExplorerDriver(options);
driver.Url = “https://google.com/ncr”;
}
}
}
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
require ‘selenium-webdriver’
options = Selenium::WebDriver::IE::Options.new
options.force_create_process_api = true
options.add_argument(’-k’)
driver = Selenium::WebDriver.for(:ie, options: options)

begin

Navigate to URL

driver.get ‘https://google.com
puts(driver.capabilities.to_json)
ensure
driver.quit
end
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const ie = require(‘selenium-webdriver/ie’);
let options = new ie.Options();
options.addBrowserCommandSwitches(’-k’);
options.addBrowserCommandSwitches(’-private’);
options.forceCreateProcessApi(true);

driver = await env.builder()
.setIeOptions(options)
.build();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}

import org.openqa.selenium.Capabilities
import org.openqa.selenium.ie.InternetExplorerDriver
import org.openqa.selenium.ie.InternetExplorerOptions

fun main() {
val options = InternetExplorerOptions()
options.useCreateProcessApiToLaunchIe()
options.addCommandSwitches("-k")
val driver = InternetExplorerDriver(options)
try {
driver.get(“https://google.com/ncr”)
val caps = driver.getCapabilities()
println(caps)
} finally {
driver.quit()
}
}
{{< / code-panel >}}
{{< / code-tab >}}

forceCreateProcessApi

强制使用CreateProcess API启动Internet Explorer.
默认值为false.

对于IE 8及更高版本,
此选项要求将 “TabProcGrowth” 注册表值设置为0.

{{< code-tab >}}
{{< code-panel language=“java” >}}
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;

public class ieTest {
public static void main(String args) {
InternetExplorerOptions options = new InternetExplorerOptions();
options.useCreateProcessApiToLaunchIe();
InternetExplorerDriver driver = new InternetExplorerDriver(options);
try {
driver.get(“https://google.com/ncr”);
Capabilities caps = driver.getCapabilities();
System.out.println(caps);
} finally {
driver.quit();
}
}
}
{{< / code-panel >}}
{{< code-panel language=“python” >}}
from selenium import webdriver

options = webdriver.IeOptions()
options.force_create_process_api = True
driver = webdriver.Ie(options=options)

Navigate to url

driver.get(“http://www.google.com”)

driver.quit()
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;

namespace ieTest {
class Program {
static void Main(string args) {
InternetExplorerOptions options = new InternetExplorerOptions();
options.ForceCreateProcessApi = true;
IWebDriver driver = new InternetExplorerDriver(options);
driver.Url = “https://google.com/ncr”;
}
}
}
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
require ‘selenium-webdriver’
options = Selenium::WebDriver::IE::Options.new
options.force_create_process_api = true
driver = Selenium::WebDriver.for(:ie, options: options)

begin

Navigate to URL

driver.get ‘https://google.com
puts(driver.capabilities.to_json)
ensure
driver.quit
end
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const ie = require(‘selenium-webdriver/ie’);
let options = new ie.Options();
options.forceCreateProcessApi(true);

driver = await env.builder()
.setIeOptions(options)
.build();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
import org.openqa.selenium.Capabilities
import org.openqa.selenium.ie.InternetExplorerDriver
import org.openqa.selenium.ie.InternetExplorerOptions

fun main() {
val options = InternetExplorerOptions()
options.useCreateProcessApiToLaunchIe()
val driver = InternetExplorerDriver(options)
try {
driver.get(“https://google.com/ncr”)
val caps = driver.getCapabilities()
println(caps)
} finally {
driver.quit()
}
}
{{< / code-panel >}}
{{< / code-tab >}}

官方链接为:https://www.selenium.dev/documentation/zh-cn/driver_idiosyncrasies/driver_specific_capabilities

关闭