「Selenium3PythonWebDriver APIソースコード分析(2):Seleniumパッケージディレクトリ構造、モジュール関数の概要」によるselenium\webdriver\remote\webdriver.py
と、モジュール内のWebDriver
クラスは一般的なWebDriverの基本的な特性を定義し、さまざまなブラウザは独自の特性に従ってWebDriver
クラスを継承します。さらに実装を行います。selenium\webdriver\firefox\webdriver.py
モジュール内のWebDriver
クラスはFireFoxWebDriverを実装し、selenium\webdriver\__init__.py
外部(from .firefox.webdriver import WebDriver as Firefox
)を介して公開されます。したがって、FireFox WebDriverを初期化するには、通常、次のメソッドを使用します。
from selenium import webdriver
driver = webdriver.Firefox()
リモートWebDriver
リモートWebDriverのクラスシグネチャは次のとおりです。
Class WebDriver(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False, file_detector=None, options=None)
コンストラクタパラメータは次のとおりです。
command_executor
:文字列またはremote_connection.RemoteConnection
オブジェクト。デフォルトは'http://127.0.0.1:4444/wd/hub'
です。desired_capabilities
-新しいセッションを作成するときcapabilities
。browser_profile
-selenium.webdriver.firefox.firefox_profile.FirefoxProfile
オブジェクト。カスタム構成ファイル。Firefoxのみ。proxy
-selenium.webdriver.common.proxy.Proxy
オブジェクト。プロキシ設定。keep_alive
-ブール値。長い接続を設定するかどうか。デフォルト値はFalse
です。file_detector
-カスタムファイルプロセッサ。デフォルト値がNone
使用されLocalFileDetector()
ます。options
-Options
クラスインスタンス。ブラウザオプションの設定。
属性は次のとおりです。
session_id
: セッションID。capabilities
:ブラウザから返されたcapabilities
コレクション。command_executor
:remote_connection.RemoteConnection
オブジェクト。コマンドの実行オブジェクト。error_handler
:ErrorHandler
オブジェクト。エラーハンドラ。
Firefox WebDriver
FirefoxWebDriverのクラスシグネチャは次のとおりです。
Class WebDriver(firefox_profile=None, firefox_binary=None, timeout=30, capabilities=None, proxy=None, executable_path="geckodriver", options=None, service_log_path="geckodriver.log", firefox_options=None, service_args=None, desired_capabilities=None, log_path=None, keep_alive=True)
Firefox WebDriverコンストラクターのパラメーターはより複雑で、関数のパラメーターの重複がいくつかあり、無駄になりそうなパラメーターがいくつかあり、最終的capabilities
には、ブラウザーに渡される辞書として構成されたこれらのパラメーターをまとめます。およびが相互に排他的
であるなどの一部のパラメーターの場合、それらの優先順位は特定のシナリオによって決定されます。続い少なくとも特定のキーワードパラメータである、と。具体的には、この時点でアプリケーションのセットとパラメータの両方が無視される場合。同様に、優先度は低くなります。firefox_profile
options.profile
capabilities
options
firefox_binary
firefox_profile
firefox_profile
options.profile
firefox_profile
options.profile
capabilities["moz:firefoxOptions"]["profile"]
options.profile
工法パラメータは次のとおりです。
firefox_profile
:FirefoxProfile
オブジェクトまたは文字列。定義されていない場合は、オペレーティングシステムの一時ディレクトリに新しいカスタム構成ファイルが生成されます。firefox_binary
:FirefoxBinary
またはFirefoxバイナリファイルのパス。定義されていない場合、オペレーティングシステムのFirefoxのデフォルトのインストール場所が使用されます。timeout
:Firefoxを起動するためのタイムアウト期間。capabilities
:desired capabilities
辞書、まもなく廃止されます。proxy
: プロキシ設定。executable_path
:geckodriver
デフォルトでシステム変数から取得されるパスpath
。options
:options.Options
例。service_log_path
:ログストレージパス。firefox_options
:同等options
、廃止されます。service_args
:ドライバサービスに渡されるパラメータリスト。desired_capabilities
:と同じcapabilities
。log_path
:同じでservice_log_path
、廃止されます。keep_alive
:長い接続を設定するかどうか。
簡略化されたクラスシグネチャは次のとおりです。
Class WebDriver(firefox_profile=None, firefox_binary=None, timeout=30, executable_path="geckodriver", options=None, service_log_path="geckodriver.log", service_args=None, desired_capabilities=None, keep_alive=True)
拡張機能のインストールとアンインストールの方法
Firefox WebDriverクラスは、拡張機能のインストールおよびアンインストールメソッドを実装します。
拡張機能をインストールする
メソッドのシグネチャは次のとおりです。def install_addon(self, path, temporary=None):
パラメータpath
は拡張機能の絶対パスです。
戻り値が拡張されid
、アンインストール時に使いやすくなりました。
場合:
import selenium.webdriver as webdriver
driver = webdriver.Firefox()
addon_id = driver.install_addon(r"\path\d.xpi")
print(addon_id)
出力は次のとおりです。{32af1358-428a-446d-873e-5f8eb5f2a72e}
つまり、d.xpi
展開されid
ます。
拡張機能をアンインストールする
メソッドのシグネチャは次のとおりです。def uninstall_addon(self, identifier):
パラメータidentifier
は、拡張機能のインストール時に返される拡張機能ですid
。