VSCode Python seleniumでエラー
VSCode上でPython seleniumでスクレイピングしようと思ったらエラーになったときのメモ。以下のエラーが発生していた。
コード(修正前)
from selenium import webdriver
import chromedriver_binary
driver = webdriver.Chrome()
driver.get('https://google.com')
エラー①:実行時 ModuleNotFoundError No module named 'selenium' のエラー

VSCodeで実行するPythonとseleniumをインストールしたPythonのバージョンがそれぞれ異なっているためエラーになっていた。
→VSCodeのPython: インタープリターを選択 でPathが通っているPythonのバージョンに合わせたら治った。
エラー②:実行時 SessionNotCreatedException のエラー

OSにインストールしているChromeのバージョンとPythonにインストールしたChromeドライバーのバージョンが異なるとエラーになるらしい。
webdriver-manager を使えばChromeのバージョンを気にしなくていいみたい。(勝手にバージョンを合わせてくれる)
→webdriver-manager をインストール
→コードを以下の通りに変更


from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager # ここを変更
driver = webdriver.Chrome(ChromeDriverManager().install()) # ここを変更
driver.get('https://google.com')
以上
以前から困っていたので解決してすっきりした。