スクレイピングもそうなのですが、自動テスト環境のした準備兼ねて
GoogleのHeadless使いたかったのでChromeDriverを。
ちょろっとやればすぐできるべ。と
高を括っていたんだけど、、プチハマり。
どれが正解かもうわからなくなってきたけど、メモだけ残しておこう。
Overview
- 自環境の確認
- pip3 でインストール
- Chromeドライバのダウングレード
- 実行
思ったよりめんどくさかった。
Input
- headless chromeをpythonで動かしてみた
- ヘッドレス Chrome ことはじめ
- SSL: CERTIFICATE_VERIFY_FAILED error with python3 on macOS 10.15
- python 3.x - geopyでジオコーダーを実行しようとしたときに[SSL:CERTIFICATE_VERIFY_FAILED]エラーを取得すると、証明書を確認して必要な出力を取得できますか?
Proccess
たまにしでかすバージョンの問題
数ヶ月とか下手すると半年以上python環境触らないので2系、3系で棲み分けてるのを忘れるのでこまる。
- python -> python2.7 / pip
- python3 -> python3.7.2 / pip3
いずれも --versionsで確認。
いつも忘れる。
python3系の環境はpython3。
pip3 が アプデできない。
19.xxxから20.xxxにアプデできなかった。
pip3 install --upgrade --user pip
installするとSSL系で怒られる。
本題のchrome driver。ここで躓く。
SSL系のエラーがでる。。
で、Certifi系の問題なのでググれば色々と出てくるんだけど、
自分の環境はbrewで入れた環境なので、Certifi installみたいなスクリプトはなく。
で、下記のスクリプトを実行することになるのだけど、
それはそれで、今度は自環境上のフォルダ構成の問題で実行できない問題。
結果として、
/usr/bin/sudo /bin/mkdir /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/etc /usr/bin/sudo /bin/ln -s /etc/ssl/ /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/etc/
# install_certifi.py # # sample script to install or update a set of default Root Certificates # for the ssl module. Uses the certificates provided by the certifi package: # https://pypi.python.org/pypi/certifi import os import os.path import ssl import stat import subprocess import sys STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH ) def main(): openssl_dir, openssl_cafile = os.path.split( ssl.get_default_verify_paths().openssl_cafile) print(" -- pip install --upgrade certifi") subprocess.check_call([sys.executable, "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"]) import certifi # change working directory to the default SSL directory os.chdir(openssl_dir) relpath_to_certifi_cafile = os.path.relpath(certifi.where()) print(" -- removing any existing file or link") try: os.remove(openssl_cafile) except FileNotFoundError: pass print(" -- creating symlink to certifi certificate bundle") os.symlink(relpath_to_certifi_cafile, openssl_cafile) print(" -- setting permissions") os.chmod(openssl_cafile, STAT_0o775) print(" -- update complete") if __name__ == '__main__': main()
実行したらChromeのバージョンが81のみと断られる
ドライバのバージョンは81。現行(その時は)79がChrome最新。
Traceback (most recent call last): File "tds_auto.py", line 8, in <module> driver = webdriver.Chrome() File "/Library/Python/3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__ desired_capabilities=desired_capabilities) File "/Library/Python/3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__ self.start_session(capabilities, browser_profile) File "/Library/Python/3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/Library/Python/3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/Library/Python/3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81
ドライバのバージョンを確認してみる。
% pip3 list WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue. To avoid this problem you can invoke Python with '-m pip' instead of running pip directly. Package Version ------------------- -------------- certifi 2019.11.28 chromedriver-binary 81.0.4044.20.0 pip 20.0.2 selenium 3.141.0 setuptools 40.8.0 six 1.12.0 urllib3 1.25.8 wheel 0.33.1
chromedriver-binary 81.0.4044.20.0
となっている。 MacのChromeは「chrome://settings/help」で、79だった。
なのでドライバも79にする必要がある。
ChromeDriverのダウンロード
https://pypi.org/search/?q=chromedriver-binary
% pip3 install chromedriver-binary==79.0.3945.36.0 WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying iss
% pip3 list WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue. To avoid this problem you can invoke Python with '-m pip' instead of running pip directly. Package Version ------------------- -------------- certifi 2019.11.28 chromedriver-binary 79.0.3945.36.0
動作確認
動いた動いた。
from selenium import webdriver import chromedriver_binary #https://hacknote.jp/archives/48685/ #https://developers.google.com/web/updates/2017/04/headless-chrome?hl=ja #---headON mode--------- driver = webdriver.Chrome() # Google検索画面にアクセス driver.get('https://www.google.co.jp/') # 検索エリア取得、ホゲホゲとか入れて。。。 searchInput = driver.find_element_by_name("q") searchInput.send_keys("hogehoge") # 検索(ただしサジェスト入るので5秒くらい待たせてます driver.implicitly_wait(5) driver.find_element_by_name("btnK").click() # 終了処理 driver.quit()
Output
- Chrome起動して
- よく見る自動化の動きして
- 終了してくれる
ちょいちょい目にしてて、サクッとできるとおもってて
30分もかからずできることをいつもこうしたつまらん環境設定でハマる。。。
調べてハマって、メモまとめて、、2時間持ってかれた。。
とほほ..
(今みたらChromeのバージョンが80になってたけどまぁ動いているからそっとしておこう)