FirefoxDriver Tips
FirefoxDriverの使い方に関する少量のメモです。
Firefoxの設定を変更する
Firefoxでは、ブラウザの設定やアドオンのデータなどは全てプロファイルに紐付けられています。プロファイルのデータは特定のディレクトリに置かれており、Firefoxの起動時に読み込まれます。ただし、FirefoxDriverを用いてFirefoxを起動した場合は既存のプロファイルは読み込まれません。常に新しいプロファイルが作成され、終了時に破棄されます。そのため、設定などはほとんど初期状態のままになります。起動時に独自の設定を反映させたい場合、Firefoxのプロファイルを扱うためのクラスが用意されているので、それを使います。
FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("general.useragent.override", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); profile.setPreference("intl.accept_languages", "en-us, en"); WebDriver driver = new FirefoxDriver(profile);
FirefoxProfile
というのが、プロファイルを扱うためのクラスです。
setPreference()
メソッドは設定値を追加、変更します。一つ目の引数に設定名、二つ目の引数に設定値を指定します。設定値のタイプには、文字列、真偽値、整数値の三種類があるので、二つ目の引数にも、String
、int
、boolean
の三種類の型の値を渡すことができます。
上記の例では、general.useragent.override
でユーザーエージェントを、intl.accept_languages
で言語設定を変更しています。
また、プロキシ設定の変更には専用のクラスが用意されているので、network.proxy.http
などを直接弄るよりは、こちらを使ったほうが楽です。
Proxy proxy = new Proxy(); proxy.setHttpProxy("proxyhost:8080"); profile.setProxyPreferences(proxy);
アドオンをインストールする
FirefoxDriverを使ってFirefoxを起動する際に、任意のアドオンをインストールすることができます。インストールするには、あらかじめアドオンのファイルをダウンロードしておく必要があります。
FirefoxProfile profile = new FirefoxProfile(); // Firebugをインストール File firebug = new File("firebug-1.8.4-fx.xpi"); profile.addExtension(firebug); profile.setPreference("extensions.firebug.currentVersion", "1.8.4"); // FireMobileSimulatorをインストール File firemobilesimulator = new File("firemobilesimulator-1.2.3-fx.xpi"); profile.addExtension(firemobilesimulator); profile.setPreference("msim.current.carrier", "DC"); profile.setPreference("msim.current.id", "1"); WebDriver driver = new FirefoxDriver(profile);
ただし、この方法を用いると、アドオンのインストールや初期化処理によって、Firefoxの起動にかかる時間が増えてしまいます。一時的にアドオンを利用するだけならあまり問題はないのですが、恒久的にアドオンを使用するようなら、あらかじめアドオンをインストールしたプロファイルを用意しておき、それを次の方法で再利用するようにしたほうが効率的です。
既存のプロファイルを利用する
前に述べた通り、FirefoxDriverを用いてFirefoxを起動すると新たなプロファイルが作成されます。ただし、既存のプロファイルを指定して起動することも可能です。既存のプロファイルを用いるには、プロファイルのデータが含まれているディレクトリを以下のように指定します。
File profileDir = new File("myprofiledir"); FirefoxProfile profile = new FirefoxProfile(profileDir); WebDriver driver = new FirefoxDriver(profile);
プロファイルディレクトリの場所でなく、プロファイル名を指定することもできます。
プロファイル名はプロファイルマネージャから確認できます。
ProfilesIni profilesIni = new ProfilesIni(); FirefoxProfile profile = profilesIni.getProfile("myprofile"); WebDriver driver = new FirefoxDriver(profile);
以下のような指定方法もあります。
System.setProperty("webdriver.firefox.profile", "myprofile"); WebDriver driver = new FirefoxDriver();
実行ファイルを指定する
Firefoxをデフォルトのインストール先と異なる位置にインストールしている場合、FirefoxDriverがFirefoxの実行ファイルを見つけられず、起動に失敗することがあります。こういった場合、自分自身で実行ファイルの位置を指定しなければなりません。
File binaryFile = new File("D:\\apps\\Mozilla Firefox\\firefox.exe"); FirefoxBinary binary = new FirefoxBinary(binaryFile); WebDriver driver = new FirefoxDriver(binary, null);
FirefoxBinary
はFirefoxの実行ファイルを扱うクラスです。実行ファイルを表すFile
オブジェクトをコンストラクタの引数として受け取ります。
Macの場合の指定方法は以下のようになります。
File binaryFile = new File("/Applications/FirefoxBeta/Firefox.app/Contents/MacOS/firefox-bin"); FirefoxBinary binary = new FirefoxBinary(binaryFile); WebDriver driver = new FirefoxDriver(binary, null);
以下の記述も同様です。
System.setProperty("webdriver.firefox.bin", "/Applications/FirefoxBeta/Firefox.app/Contents/MacOS/firefox-bin"); WebDriver driver = new FirefoxDriver();
一時ファイルを残す
あまり利用する場面はないと思いますが、WebDriverが作成する一時ファイルを削除しないようにすることができます。これにより、FirefoxDriverが作成した一時プロファイルを削除せずに残すことも可能です。
System.setProperty("webdriver.reap_profile", "false"); WebDriver driver = new FirefoxDriver();
ログをファイルに出力する
WebDriverが吐いたログをファイルに出力することができます。何かしら不審な挙動をしている際には、役に立つ情報になるかもしれません。
FirefoxProfile profile = new FirefoxProfile(); File logFile = new File("webdriver.log"); profile.setPreference("webdriver.log.file", logFile.getAbsolutePath()); WebDriver driver = new FirefoxDriver(profile);
新しい方法でのページの読み込み検出
Seleniumのバージョン2.9からの実験的な機能として、ページの読み込みが完全に完了するまで待機しない設定が可能になったらしいです。しかし、自分の環境ではうまく動きませんでした…。
FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("webdriver.load.strategy", "fast"); driver = new FirefoxDriver(profile);