Firefox固有の機能

これらは、Mozilla 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)
  

サービス

すべてのブラウザに共通のサービス設定は、サービスのページで説明されています。

ログ出力

ドライバログを取得することは、さまざまな問題をデバッグするのに役立ちます。Serviceクラスを使用すると、ログの出力先を指定できます。ログ出力は、ユーザーがどこかに指示しない限り無視されます。

ファイル出力

特定のファイルに保存するようにログ出力を変更するには

    FirefoxDriverService service =
        new GeckoDriverService.Builder().withLogFile(logLocation).build();

: Javaでは、システムプロパティによるファイル出力の設定も可能です。
プロパティキー: GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY
プロパティ値: ログファイルへのパスを表す文字列

Selenium v4.11

    service = webdriver.FirefoxService(log_output=log_path, service_args=['--log', 'debug'])

コンソール出力

コンソールに表示するようにログ出力を変更するには

Selenium v4.10

    FirefoxDriverService service =
        new GeckoDriverService.Builder().withLogOutput(System.out).build();

: Javaでは、システムプロパティによるコンソール出力の設定も可能です。
プロパティキー: GeckoDriverService.GECKO_DRIVER_LOG_PROPERTY
プロパティ値: DriverService.LOG_STDOUT または DriverService.LOG_STDERR

Selenium v4.11

    service = webdriver.FirefoxService(log_output=subprocess.STDOUT)

ログレベル

利用可能なログレベルは7つあります: fatalerrorwarninfoconfigdebugtrace。ログが指定されている場合、レベルはデフォルトで info になります。

-v-log debug と同等であり、-vvlog trace と同等であることに注意してください。したがって、この例はログレベルを一般的に設定するためのものです。

Selenium v4.10

    FirefoxDriverService service =
        new GeckoDriverService.Builder().withLogLevel(FirefoxDriverLogLevel.DEBUG).build();

: Javaでは、システムプロパティによるログレベルの設定も可能です。
プロパティキー: GeckoDriverService.GECKO_DRIVER_LOG_LEVEL_PROPERTY
プロパティ値: FirefoxDriverLogLevel enumの文字列表現

Selenium v4.11

    service = webdriver.FirefoxService(log_output=log_path, service_args=['--log', 'debug'])

Selenium v4.10

      service.args += %w[--log debug]

切り捨てられたログ

ドライバは、大きなバイナリの文字列表現を含む、ドライバに送信されるすべてのものをログに記録するため、Firefoxはデフォルトで行を切り捨てます。切り捨てをオフにするには

Selenium v4.10

    FirefoxDriverService service =
        new GeckoDriverService.Builder().withTruncatedLogs(false).build();

: Javaでは、システムプロパティによるログレベルの設定も可能です。
プロパティキー: GeckoDriverService.GECKO_DRIVER_LOG_NO_TRUNCATE
プロパティ値: "true" または "false"

Selenium v4.11

    service = webdriver.FirefoxService(service_args=['--log-no-truncate', '--log', 'debug'], log_output=log_path)

Selenium v4.10

      service.args << '--log-no-truncate'

プロファイルルート

プロファイルのデフォルトディレクトリは、システムのテンポラリディレクトリです。そのディレクトリへのアクセス権がない場合、またはプロファイルを特定の場所に作成したい場合は、プロファイルルートディレクトリを変更できます。

Selenium v4.10

    FirefoxDriverService service =
        new GeckoDriverService.Builder().withProfileRoot(profileDirectory).build();

: Javaでは、システムプロパティによるログレベルの設定も可能です。
プロパティキー: GeckoDriverService.GECKO_DRIVER_PROFILE_ROOT
プロパティ値: プロファイルルートディレクトリへのパスを表す文字列

    service = webdriver.FirefoxService(service_args=['--profile-root', temp_dir])

Selenium v4.8

      service.args += ['--profile-root', root_directory]

特別な機能

一部のブラウザには、ブラウザに固有の追加機能が実装されています。

アドオン

Chromeとは異なり、Firefox拡張機能はこのIssueで述べられているように、機能の一部として追加されるのではなく、ドライバの起動後に作成されます。

以下の例は、ローカルWebDriver用です。リモートWebDriverについては、Remote 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)

Selenium v4.5

            driver.UninstallAddOn(extensionId);
      driver.uninstall_addon(extension_id)
    await driver.uninstallAddon(id);

署名なしインストール

未完成または未公開の拡張機能を扱う場合、署名されていない可能性があります。そのため、「一時的」にのみインストールできます。これは、zipファイルまたはディレクトリのいずれかを渡すことで行うことができます。ディレクトリの例を次に示します。

    driver.installExtension(path, true);
    driver.install_addon(addon_path_dir, temporary=True)

Selenium v4.5

            driver.InstallAddOnFromDirectory(Path.GetFullPath(extensionDirPath), true);

Selenium v4.5

      driver.install_addon(extension_dir_path, 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'