Firefox特有の機能
Selenium 4 には Firefox 78 以降が必要です。 常に最新バージョンの geckodriver を使用することをお勧めします。
オプション
全ブラウザに共通のCapabilityについては、オプションページで説明しています。
Firefox に固有のCapabilityは、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();
さまざまなCapabilityを備えた一般的な使用例をいくつか示します。
引数
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の両方がコンピューターに存在する場合、
製品版の代わりに 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 RemoteWebDriver(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 = RemoteWebDriver(options)
Note: Whether you create an empty FirefoxProfile
or point it to the directory of your own profile, Selenium
will create a temporary directory to store either the data of the new profile or a copy of your existing one. Every
time you run your program, a different temporary directory will be created. These directories are not cleaned up
explicitly by Selenium, they should eventually get removed by the operating system. However, if you want to remove
the copy manually (e.g. if your profile is large in size), the path of the copy is exposed by the FirefoxProfile
object. Check the language specific implementation to see how to retrieve that location.
If you want to use an existing Firefox profile, you can pass in the path to that profile. Please refer to the official Firefox documentation for instructions on how to find the directory of your profile.
サービス
すべてのブラウザに共通するサービス設定は、Service pageに記載されています。
ログ出力
ドライバーログを取得することは、さまざまな問題のデバッグに役立ちます。サービスクラスを使用すると、ログの保存先を指定できます。ログ出力は、ユーザーがどこかに指定しない限り無視されます。
ファイル出力
特定のファイルにログ出力を保存するには:
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
service = webdriver.FirefoxService(log_output=subprocess.STDOUT)
ログレベル
利用可能なログレベルは7つあります: fatal
, error
, warn
, info
, config
, debug
, trace
。
ロギングが指定されている場合、デフォルトのレベルは info
になります。
-v
iは -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の拡張機能はCapabilityの一部として追加されるのではなく、ドライバーの起動後に作成されます。
Chromeとは異なり、Firefoxの拡張機能はこの問題に記載されているように、機能の一部として追加されるのではなく、ドライバーの起動後に作成されます。
T以下の例はローカルWebDriver用です。リモートWebDriverについては、Remote WebDriverページを参照してください。
インストール
Mozilla Add-Onsページ から取得する署名付き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については、Remote 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については、Remote 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'
Note: As of Firefox 138, geckodriver needs to be started with the argument --allow-system-access
to switch the context to CHROME
.