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
參數接受要使用的瀏覽器替代位置的路徑。例如,使用此參數,您可以讓 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)
服務
所有瀏覽器通用的服務設定都在服務頁面中描述。
日誌輸出
取得驅動程式日誌對於除錯各種問題很有幫助。「服務」類別可讓您指定日誌的去向。除非使用者指示,否則日誌輸出會被忽略。
檔案輸出
若要變更日誌輸出以儲存到特定檔案
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'