LoginSignup
0
2

More than 3 years have passed since last update.

Selenium Python クロームドライバー(chromedriver.exe)の自動インストール

Last updated at Posted at 2021-05-13

環境:WIndows10
pythonバージョン:3.8.32

chromedriver.exeの自動インストール(更新も自動)

以下の本にchromedriver.exeの自動インストールの方法があったので自分の環境に合わせてメモします。↓
chromedriver.exeにパスが通ってなくても、プログラムをexeファイル化しても、自動でインストールされました。

スクレイピング・ハッキングラボ:アマゾンのページ

webdriver_manager

webdriver_manager

webdriver_managerを使うとchromedriver.exeを自動でインストールしてくれます。

chromedriver.exeを自動でインストールしてヤフーに接続するサンプル

python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import time

options = Options()

options.add_argument('--hide-scrollbars')
options.add_argument('--incognito')  #シークレットモードのオプション

#旧 pythonの実行ファイルと同じ場所に   chromedriver.exeを設置してた。
#file_path = os.path.join(os.path.dirname(sys.argv[0]), "chromedriver.exe") 
#driver = webdriver.Chrome(options=options, executable_path=file_path)

#新  わざわざchromedriver.exeを設置しなくても自動でインストール更新されます。
driver=webdriver.Chrome(ChromeDriverManager().install(),options=options)
driver.get("http://yahoo.co.jp")   

time.sleep(5)
driver.quit()


上のソースを実行するにはwebdriver_managerのインストールが必要です。
自分のWindwos10環境の場合

py -m pip で webdriver_managerをインストール

pip
py -m pip install webdriver_manager

Selenium+クロームドライバーで作業していて必ず必要なのがchromedriver.exeの変更やインストール。
3か月に1回程度は更新が必要なようで、スクレイピングをタイマー起動しても、エラーで止まってしまうことしばしば。コピーして別のPCで使うにもchromedriver.exeがなくて、とまったりということがなくなる予定です。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2