0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WSL環境のRubyにて、selenium(Chrome)を利用する

Last updated at Posted at 2021-01-14

WSL環境のRubyにて、selenium(Chrome)を利用する

事前準備

必要なライブラリのインストール

sudo apt-get update
sudo apt-get install -y libappindicator1 fonts-liberation

ヘッドレスモードを使用できるchromeをインストール

sudo apt install gdebi-core wget unzip
sudo gdebi google-chrome-stable_current_amd64.deb

Chromeのバージョンを確認

google-chrome --version

chromedriverのインストール

現在インストールされている google-chrome に対応する chromedriver をダウンロードする
https://sites.google.com/a/chromium.org/chromedriver/

curl -O https://chromedriver.storage.googleapis.com/87.0.4280.88/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod +x chromedriver
sudo mv -f chromedriver /usr/local/share/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver

動作確認

  • パッケージの変更
Gemfile
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem "webdriver"
gem "selenium-webdriver"
  • パッケージの反映
bundle install
  • Rubyのサンプルコード
test.rb
require "selenium-webdriver"

options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')

driver = Selenium::WebDriver.for(
  :chrome,
  options: options
)
driver.navigate.to "https://www.google.com/"

puts driver.title

driver.quit
  • 動作確認
ruby test.rb

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?