chromedriver インストールメモ

2018-08-16 15:07 (5年前) ytyng

Ubuntu 16.04

参考 EC2 UbuntuでGoogle Chromeをヘッドレス実行してスクリーンショットを採取する手順 - Qiita
python 2.7 - Unknown error: Chrome failed to start: exited abnormally - Stack Overflow

Chrome

curl -O https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt update
sudo apt -f install -y

テスト

cd /tmp
google-chrome --headless --disable-gpu --screenshot https://www.example.com/
imgcat screenshot.png # imgcat については後述

chromedriver

curl https://chromedriver.storage.googleapis.com/LATEST_RELEASE

curl -O  https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip

unzip chromedriver_linux64.zip

sudo mv chromedriver /usr/local/bin/

テスト

chromedriver --verbsose &
curl -d '{ "desiredCapabilities": {"browserName": "chrome", "chromeOptions": {"args": [ "--headless" ]} }}' http://127.0.0.1:9515/session

sessionId が返ってくる
ちなみに、--headless をつけない場合

xvfb-run chromedriver --verbsose &

とする必要がある


ページを開く

curl -d '{"url":"https://www.example.com"}' http://127.0.0.1:9515/session/<session-id>/url


スクリーンショット

curl http://127.0.0.1:9515/session/<session-id>/screenshot | python3 -c "import sys, json; print(json.load(sys.stdin)['value'])"|base64 -d |imgcat

※ screenshot コマンドを実行 ➝ 結果が Base64 を含む JSON で返ってくるので Python でパース ➝ デコードしてバイナリ化 ➝ imgcat で iterm2 に表示

※動かない場合 --no-sandbox --disable-dev-shm-usage なんかも気にしてみる

メモ: Webセキュリティを無効 --ignore-certificate-errors --disable-web-security


参考: https://www.pawangaria.com/post/automation/browser-automation-from-command-line/

Python ライブラリ

pyvirtualdisplay 必要

sudo apt install xvfb
pip install selenium chromedriver pyvirtualdisplay


テストコード

from pyvirtualdisplay import Display
display = Display(visible=0, size=(1280, 800))
display.start()

from selenium.webdriver import Chrome, ChromeOptions

options = ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = Chrome(options=options)

driver.get('https://example.com')
driver.get_screenshot_as_file('/tmp/screenshot.png')

おまけ サーバ上の画像を見る

iTermを使っているなら

https://gist.github.com/wesbos/eac5f93478002312db1f このスクリプト

sudo curl -o /usr/local/bin/imgcat -O https://raw.githubusercontent.com/gnachman/iTerm2/master/tests/imgcat && sudo chmod +x /usr/local/bin/imgcat

で、imgcat をサーバ上にインストールしておけば、

imgcat /tmp/screenshot.png

で画像がターミナルに表示される。

Macの場合の注意: サイトからバイナリ落として使いましょう

Mac で、brew からインストールした chromedriver を使っている場合、get メソッドの際

data:;

というURLが開いて

    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

で停止する場合があります。

brew uninstall --force chromedriver

で chromedriver をアンインストールし、

http://chromedriver.chromium.org/downloads ここから最新版の Mac 用バイナリをダウンロードし、ウイルススキャンしてから /usr/local/bin/ にでも入れて使うといいでしょう。

トラブルシューティング

Ubuntuで

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.15.0-1023-aws x86_64)

が出る

https://stackoverflow.com/questions/50790733/unknown-error-devtoolsactiveport-file-doesnt-exist-error-while-executing-selen
ここを参考に

System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--no-sandbox"); // Bypass OS security model
WebDriver driver = new ChromeDriver(options);
driver.get("https://google.com");

このようなオプションを追加してみましょう。これは Python のコードではないですが。

私は、disable-infobars 入れたらエラーでなくなりました。

現在の評価: 2

コメント

アーカイブ

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011