LoginSignup
2
0

More than 3 years have passed since last update.

[Linux : Ruby] seleniumでブラウザドライバのパスを明示せずにドライバを使用する

Posted at

環境

Ubuntu 18.04 LTS
Ruby 2.6.5
selenium-webdriver 3.142.7

はじめに

rubyでselenium-webdriverを使う際、linuxではパスが通っているディレクトリにドライバファイルを置いておけば、ドライバのパスを明示しなくてもドライバを使うことができます。そのときの注意点とともに方法をまとめておきます。

パスが通っているディレクトリにドライバを置く

今回は例としてChromeのドライバを使います。以下からはドライバをインストールしたものとして進めます。

まずは環境変数を見てパスが通っているディレクトリを確認します。

$ printenv PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

「:」を区切りとして、パスが通っているディレクトリが表示されます。
今回は「usr/local/bin」にドライバファイルを置きました。

/usr/local/bin$ ls
chromedriver

この状態でseleniumでchromeのドライバを呼んでやればパスを指定せずに使うことができます。

test.rb
require 'selenium-webdriver'

d = Selenium::WebDriver.for :chrome
d.get('http://****')

ドライバファイル名は「chromedriver」でないと認識されない

ドライバファイル名は「chromedriver」でないと実行時にエラーとなります。
自分はver.83のドライバをインストールしたので、ドライバインストール時に「chromedriver83」として保存していたのですが、エラーとなりつまづきました。

/usr/local/bin$ ls
chromedriver83

スクリプトを実行するとエラー

 Unable to find chromedriver. Please download the server from (Selenium::WebDriver::Error::WebDriverError)

chromedriverが見つからないと言われます

パス指定であれば別ファイル名でも可

スクリプトでドライバのパスを明示する方法であれば、別ファイル名でも大丈夫です。

test.rb
require 'selenium-webdriver'

Selenium::WebDriver::Chrome::Service.driver_path = '/usr/local/bin/chromedriver83'
d = Selenium::WebDriver.for :chrome
d.get('http://****')

ちなみにこの方法だと、パスが通っているかどうかは関係ありません。

2
0
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
2
0