0
0

SeleniumでChrome driverを立ち上げた際にすぐにクラッシュしてしまった時の対処法

Last updated at Posted at 2024-02-12

はじめに

こんばんは、ダンです。
以前乃木坂46のライブを自動で応募するシステムを作ったのですが、
記事URL:https://qiita.com/dandan5656/items/d1fe82d2d7594ebb5fa1
その際にSeleniumでChrome driverを立ち上げた際にすぐにクラッシュしてしまい、3-4時間ほど躓いてしまったので、対処法を共有したいと思います。

状況

スクレイピングに必要なSeleniumとブラウザを立ち上げるためにChrome driverをインストールし、

pip3 install selenium
brew install chromedriver

ブラウザを立ち上げました。

from selenium import webdriver
browser = webdriver.Chrome()

その際にターミナルにエラー分は表示されないのですが、Chromeがすぐに閉じてしまったり、クラッシュしたりしてしまいました。

解決法

Chrome Driverを通すためのパスが正しく通っていなかったため、クラッシュしていたみたいです。
以下のように直接ChromeDriverのパスを直接指定すると解決しました。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService

# ChromeDriverのパスを指定
chrome_driver_path = '/usr/local/bin/chromedriver'

# ChromeDriverの初期化(Serviceオブジェクトを使用)
service = ChromeService(executable_path=chrome_driver_path)
browser = webdriver.Chrome(service=service)

# ウェブページを開くなどの操作を追加

# ブラウザを閉じる
browser.quit()

まとめ

ChromeDriverへのパスが間違っていて、Seleniumが正しくブラウザを制御できず、エラーが発生してクラッシュしていたと考察します。
パスを正しく設定することで、Seleniumが必要なファイルにアクセスできるようになり、ブラウザの制御が正常に行われるようになりました!
ネットで調べても全然解決法が出て来ず苦戦してしまっていたので、誰かの役に立てば幸いです!

0
0
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
0