【Selenium】Firefox を Python から操作してみる

PythonFirefox を操作する時は、geckodriver が必要になる。

github.com

ダウンロードしたら、/usr/local/bin に入れておく(または、任意の場所に入れてPATHを通しておく)。

Firefox Developer Edition.app をダウンロードしておくとよい。

www.mozilla.org

↓こちらはテストコード

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
# ブラウザーを起動
opts = Options()
opts.binary_location = '/Applications/Firefox Developer Edition.app/Contents/MacOS/firefox'
# opts.headless = True
driver = webdriver.Firefox(options=opts)
# Google検索画面にアクセス
driver.get('https://www.google.co.jp/')
# htmlを取得・表示
html = driver.page_source
# print(html)
# ブラウザーを終了
driver.quit()

DeprecationWarning: use options instead of chrome_options (or firefox_options) のエラーが出たら、

driver = webdriver.Chrome(chrome_options=options) ではなく

driver = webdriver.Chrome(options=options)にする

tokyo-engineer.com

Firefox の デフォルト のプロファイルフォルダーの場所は、

~/Library/Application Support/Firefox/Profiles

返信を残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA