Firefox 特定功能
Selenium 4 要求 Firefox 78 或更高版本。建议始终使用最新版本的 geckodriver。
选项
所有浏览器通用的功能在 选项页面 中描述。
Firefox 特有的功能可以在 Mozilla 的 firefoxOptions 页面找到。
使用基本定义的选项启动 Firefox 会话如下所示
FirefoxOptions options = new FirefoxOptions();
driver = new FirefoxDriver(options); options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(options=options) var options = new FirefoxOptions();
driver = new FirefoxDriver(options); options = Selenium::WebDriver::Options.firefox
@driver = Selenium::WebDriver.for :firefox, options: options driver = new Builder()
.forBrowser(Browser.FIREFOX)
.setFirefoxOptions(options)
.build();参数
args 参数用于启动浏览器时使用的一系列命令行开关。
常用的 args 包括 -headless 和 "-profile", "/path/to/profile"
向选项添加参数
options.addArguments("-headless"); options.add_argument("-headless") options.AddArgument("-headless"); options.args << '-headless' .setFirefoxOptions(options.addArguments('--headless'))在指定位置启动浏览器
binary 参数接收要使用的浏览器备用位置的路径。例如,使用此参数,当您的计算机上同时存在生产版本和 Firefox Nightly 时,您可以使用 geckodriver 来驱动 Firefox Nightly。
向选项添加浏览器位置
options.setBinary(getFirefoxLocation()); options.binary_location = firefox_bin options.BinaryLocation = GetFirefoxLocation(); options.binary = firefox_location配置文件
有几种方法可以处理 Firefox 配置文件。
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
profile.setPreference("javascript.enabled", "False");
options.setProfile(profile);
driver = new FirefoxDriver(options); 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
driver = webdriver.Firefox(options=options)var options = new FirefoxOptions();
var profile = new FirefoxProfile();
options.Profile = profile;
var driver = new FirefoxDriver(options);
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = '/tmp/webdriver-downloads'
options = Selenium::WebDriver::Firefox::Options.new(profile: profile)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();
val options = FirefoxOptions()
options.profile = FirefoxProfile()
driver = FirefoxDriver(options)
服务
所有浏览器通用的服务设置在 服务页面 中描述。
日志输出
获取驱动程序日志有助于调试各种问题。Service 类允许您指定日志的输出位置。除非用户将其定向到某个位置,否则将忽略日志输出。
文件输出
要更改日志输出以保存到特定文件
FirefoxDriverService service =
new GeckoDriverService.Builder().withLogFile(logLocation).build();注意:Java 还允许通过系统属性设置文件输出
属性键:GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY
属性值:表示日志文件路径的字符串
service = webdriver.FirefoxService(log_output=log_path, service_args=['--log', 'debug'])控制台输出
要更改日志输出以显示在控制台中
FirefoxDriverService service =
new GeckoDriverService.Builder().withLogOutput(System.out).build();注意:Java 还允许通过系统属性设置控制台输出;
属性键:GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY
属性值:DriverService.LOG_STDOUT 或 DriverService.LOG_STDERR
日志级别
有 7 个可用的日志级别:fatal、error、warn、info、config、debug、trace。如果指定了日志记录,则级别默认为 info。
请注意,-v 等效于 -log debug,-vv 等效于 log trace,因此此示例仅用于通用设置日志级别
FirefoxDriverService service =
new GeckoDriverService.Builder().withLogLevel(FirefoxDriverLogLevel.DEBUG).build();注意:Java 还允许通过系统属性设置日志级别
属性键:GeckoDriverService.GECKO_DRIVER_LOG_LEVEL_PROPERTY
属性值:FirefoxDriverLogLevel 枚举的字符串表示形式
service = webdriver.FirefoxService(log_output=log_path, service_args=['--log', 'debug'])截断的日志
驱动程序会记录发送给它的所有内容,包括大型二进制文件的字符串表示形式,因此 Firefox 默认会截断行。要关闭截断
FirefoxDriverService service =
new GeckoDriverService.Builder().withTruncatedLogs(false).build();注意:Java 还允许通过系统属性设置日志级别
属性键:GeckoDriverService.GECKO_DRIVER_LOG_NO_TRUNCATE
属性值:"true" 或 "false"
service = webdriver.FirefoxService(service_args=['--log-no-truncate', '--log', 'debug'], log_output=log_path)配置文件根目录
配置文件的默认目录是系统临时目录。如果您无权访问该目录,或者希望在特定位置创建配置文件,则可以更改配置文件根目录
FirefoxDriverService service =
new GeckoDriverService.Builder().withProfileRoot(profileDirectory).build();注意:Java 还允许通过系统属性设置日志级别
属性键:GeckoDriverService.GECKO_DRIVER_PROFILE_ROOT
属性值:表示配置文件根目录路径的字符串
service = webdriver.FirefoxService(service_args=['--profile-root', temp_dir])特殊功能
一些浏览器已经实现了它们独有的附加功能。
附加组件
与 Chrome 不同,Firefox 扩展程序不是作为 此问题 中提到的功能的一部分添加的,而是在启动驱动程序后创建的。
以下示例适用于本地 webdriver。对于远程 webdriver,请参阅 远程 WebDriver 页面。
安装
您从 Mozilla 附加组件页面 获取的已签名 xpi 文件
driver.installExtension(xpiPath); driver.install_addon(addon_path_xpi) driver.InstallAddOnFromFile(Path.GetFullPath(extensionFilePath)); driver.install_addon(extension_file_path) let id = await driver.installAddon(xpiPath);卸载
卸载附加组件需要知道其 ID。该 ID 可以从安装附加组件时的返回值中获得。
driver.uninstallExtension(id); driver.uninstall_addon(id) driver.uninstall_addon(extension_id) await driver.uninstallAddon(id);未签名安装
当使用未完成或未发布的扩展程序时,它很可能未签名。因此,它只能作为“临时”安装。这可以通过传入 zip 文件或目录来完成,这里有一个目录示例
driver.installExtension(path, true); driver.install_addon(addon_path_dir, temporary=True) driver.InstallAddOnFromDirectory(Path.GetFullPath(extensionDirPath), true); let id = await driver.installAddon(xpiPath, true);完整页面截图
以下示例适用于本地 webdriver。对于远程 webdriver,请参阅 远程 WebDriver 页面。
File screenshot = driver.getFullPageScreenshotAs(OutputType.FILE); driver.save_full_page_screenshot("full_page_screenshot.png") screenshot = driver.save_full_page_screenshot(File.join(dir, 'screenshot.png'))上下文
以下示例适用于本地 webdriver。对于远程 webdriver,请参阅 远程 WebDriver 页面。
((HasContext) driver).setContext(FirefoxCommandContext.CHROME);
driver.executeScript("console.log('Inside Chrome context');"); with driver.context(driver.CONTEXT_CHROME):
driver.execute_script("console.log('Inside Chrome context');") driver.context = 'content'



