概要
Ubuntu上でSeleniumを動かした時のメモ・備忘録。ネット上でサンプルコードが見つかりやすいpythonでやることにした。 環境は、 Selenium + Chromedriver + Ubuntu14 + Python3
仮想環境構築
Vagrantを利用します。
https://qiita.com/w2-yamaguchi/items/191830191f8af05ac4dd https://qiita.com/hidekuro/items/385bcc4b9eb43945751dこの辺りを参考にしました。
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.box_version = "20180406.0.0"
config.vm.network "forwarded_port", guest: 3000, host: 3000
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "2048"
vb.customize [
"modifyvm", :id,
"--vram", "256",
"--clipboard",
"--draganddrop",
"--cpus", "2",
"--ioapic", "on"
]
end
end
次にデスクトップ環境を導入します。
sudo apt-get -y install ubuntu-desktop
デスクトップが重い場合はメモリの割り当てを増やします。 デフォルトのキーボードがUS配列だったのでこれも変更します。
sudo dpkg-reconfigure keyboard-configuration
そして Generic 105-key (Intl) PC → Japanese → Japanese → The default for the keyboard layout → No compose key ここを参考にしました。 https://qiita.com/jjjiii26/items/af134896483ae9d32a7d
Selenium導入
python3環境がなかったのでpythonを導入。
sudo apt install python3-pip
Chromeのインストール
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
Seleniumのインストール
sudo apt-get install python3-selenium
Chromeドライバのインストール
wget https://chromedriver.storage.googleapis.com/2.31/chromedriver_linux64.zip
unzip chromedriver_linux64.zip -d ~/bin/
これで準備完了。
また、
sudo apt-get search selenium
をすると下のように関連パッケージが出てくるので試してみたいと思います。
chromium-chromedriver/trusty-updates,trusty-security,now 65.0.3325.181-0ubuntu0.14.04.1 amd64 [installed]
WebDriver driver for the Chromium Browser
libtest-www-selenium-perl/trusty 1.36-1 all
Perl test framework using Selenium Remote Control
phpunit-selenium/trusty 1.2.6-3 all
Selenium RC integration for PHPUnit
python-selenium/trusty,now 2.25.0-0ubuntu1 all [installed]
python bindings for Selenium
ruby-childprocess/trusty 0.3.9-2 all
Ruby library for controlling external programs running in the background
実行
from selenium import webdriver
driver = webdriver.Chrome("/usr/bin/chromedriver")
driver.get('https://google.co.jp')
googleのトップに遷移します。
現在のUbuntuで動かす最小例
この記事のUbuntu 14.04、Python 2系、Selenium 2系の手順は現在では古くなっています。Selenium 4の各言語バインディングにはSelenium Managerが同梱され、通常はChromeDriverを手動ダウンロードする必要がありません。
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
options.add_argument("--window-size=1280,900")
with webdriver.Chrome(options=options) as driver:
driver.get("https://example.com/")
print(driver.title)
print(driver.find_element(By.TAG_NAME, "h1").text)
webdriver.Chrome() の呼び出し時にブラウザとdriverを検出し、必要なら適合するdriverを取得・cacheします。社内Proxyやオフライン環境ではSelenium ManagerのProxy、mirror、offline設定か、管理済みdriverの明示指定を検討してください。
CI・コンテナでの注意
- ブラウザと共有ライブラリをコンテナイメージへ含める
- 固定sleepではなくWebDriverWaitで要素の状態を待つ
- 失敗時にスクリーンショット、ページソース、ブラウザログを保存する
- 外部サイトではなく管理下のテスト環境を対象にする
- browser/driverのバージョンをログへ出す
root実行を理由に安易に --no-sandbox を付けるのではなく、非rootユーザーで動かせるコンテナ構成を優先してください。