seleniumとchromedriverのインストール
$ apt install -y chromium-browser $ apt-get install chromium-chromedriver $ pip install -U selenium
ダメなケース
from selenium import webdriver browser = webdriver.Chrome() browser.get('https://...')
インストールしたchromedriverのPATH指定が必要みたい
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
selenium.webdriverにchromedriverのパスを通す
from selenium import webdriver from selenium.webdriver.chrome.options import Options CHROME_BIN = "/usr/bin/chromium-browser" CHROME_DRIVER = '/usr/lib/chromium-browser/chromedriver' options = Options() options.binary_location = CHROME_BIN options.add_argument('--headless') browser = webdriver.Chrome(CHROME_DRIVER, options=options) browser.get('https://...')
これで実行しても、以下のようなエラーが出る...
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.)
以下のようなオプションを追記して実行すると動く
from selenium import webdriver from selenium.webdriver.chrome.options import Options CHROME_BIN = "/usr/bin/chromium-browser" CHROME_DRIVER = '/usr/lib/chromium-browser/chromedriver' options = Options() options.binary_location = CHROME_BIN options.add_argument("--disable-dev-shm-usage") options.add_argument("start-maximized") options.add_argument("disable-infobars") options.add_argument("--disable-extensions") options.add_argument("--disable-gpu") options.add_argument("--no-sandbox") options.add_argument('--headless') browser = webdriver.Chrome(CHROME_DRIVER, options=options) browser.get('https://...')
サンプル
参考にしたページ
Raspberry PiにSeleniumとchromedriverでブラウザ操作 | MIKI-IE.COM(みきいえMIKIIE)
chromedriver インストールメモ | ytyng.com
リンク
リンク