ウェブのフォーム入力テストの自動化をしたくて、Seleniumを導入しようとしたところ、下記のエラーが出て起動してくれない
環境:
Linux Ubuntu 18.04LTS
Python 3.7.3
pip 19.0.3
Google Chrome 75.0.3770.142
ChromeDriver 75.0.3770.140 (2d9f97485c7b07dc18a74666574f19176731995c-refs/branch-heads/3770@{#1155})
(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.)
結論としては環境変数$DISPLAYの設定が漏れていたからだった。
echo 'export DISPLAY="${DISPLAY:-:0}"' >> ~/.zshrc
にて解決。
だめな人は単にこれでも動くかも
export DISPLAY=:0.0
解決までの道筋
はじめに下記のURLを参考にSeleniumをインストールした
Python + Selenium で Chrome の自動操作を一通り – Qiita
しかし動かない…Firefoxの方も試してみたけど動かない。
次に下記のURLを参考にスクリーンショットを取得してみる
UbuntuのCLI環境でGoogleChromeを使ってウェブサイトのスクリーンショットを取得する – Qiita
たしかにGoogleの検索画面のスクリーンショットが取得できた。
chromedriverは動いていることを確認
ということは、ブラウザの起動まわりで上手く行っていないということが伺える。
そこでchromeを単体で実行(/usr/bin/google-chrome)してみる。
(google-chrome:29464): Gtk-WARNING **: 17:41:15.291: cannot open display:
というエラーが出て起動しないことで、DISPLAY周りで異常が起こっていることを突き止める。
最終的に下記URLを参考に.zshrcでDISPLAY環境変数を設定できていなかったことに気づく。
ubuntu で root 時に cannot open display となる件 – たつこの部屋
#google.py
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.google.com/')
time.sleep(5)
search_box = driver.find_element_by_name("q")
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5)
driver.quit()
参考文献など :
$DISPLAYの設定: ubuntu で root 時に cannot open display となる件 – たつこの部屋
環境変数$DISPLAYについて: DISPLAY – なんなんなん行く?
seleniumのインストール: UbuntuのCLI環境でGoogleChromeを使ってウェブサイトのスクリーンショットを取得する – Qiita
この記事へのコメントはありません。