Use selenium (Chrome) in Ruby in WSL environment

Use selenium (Chrome) in Ruby in WSL environment

Advance preparation

Installation of required libraries

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

Install chrome that can use headless mode

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

Check Chrome version

google-chrome --version

chrome driver installation

Download the chromedriver for the currently installed google-chrome 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

Operation check

--Package change

Gemfile


source "https://rubygems.org"

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

gem "webdriver"
gem "selenium-webdriver"

--Package reflection

bundle install

--Ruby sample code

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

--Operation check

ruby test.rb

reference

--How to test Ruby with Selenium WebDriver [For beginners] https://techacademy.jp/magazine/18704

--Run Headless chrome on Ruby and Selenium on Ubuntu/Linux https://qiita.com/meguroman/items/41ca17e7dc66d6c88c07

-[Complete version] Cheat sheet that automatically operates (crawling / scraping) the browser with Python and Selenium https://tanuhack.com/selenium/

Recommended Posts

Use selenium (Chrome) in Ruby in WSL environment
Use selenium (Firefox) in Ruby in WSL environment
Install ruby serial port in WSL environment
Update Ruby in Unicorn environment
Use ruby variables in javascript.
WSL2 + Ubuntu 20.04 Install Vivado in LTS environment
[Docker] Use environment variables in Nginx conf
How to use environment variables in RubyOnRails
I want to use arrow notation in Ruby
Class in Ruby
Heavy in Ruby! ??
[Rails] How to use PostgreSQL in Vagrant environment
Use Shift Right and Shift Left properly in Selenium
[Ruby] How to use standard output in conditional branching
People who don't usually use Ruby build a Ruby environment
Install Rust in WSL2 Ubuntu environment and build WASM build environment
Beginners use ubuntu in windows to prepare rails environment
Use Coveralls with GitHub Actions in a Ruby repository
Handling of date and time in Ruby. Use Date and Time properly.
Use java.time in Jackson
Use Interceptor in Spring
Use OpenCV in Java
Use MouseListener in Processing
Use images in Rails
Output triangle in Ruby
Use PostgreSQL in Scala
Variable type in ruby
Use PreparedStatement in Java
Fast popcount in Ruby
About docker.credentials.errors.StoreErrorTraceback in wsl2
Install / run a standalone robot simulator in wsl2 / docker environment!
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
Use Java Web Start in an OpenJDK environment on Windows
Use docker-compose.yml which is different for each environment in Makefile