I'm running Ubuntu 16.04 and I'm trying to run a headless Chrome browser in ruby with chromedriver
.
I've installed chromedriver
on Ubuntu using these instructions and then I run this via the ruby irb
console:
require 'selenium-webdriver'
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
@driver = Selenium::WebDriver.for(:chrome, options: options)
Traceback (most recent call last):
10: from /home/weefee/.rvm/rubies/ruby-2.5.1/bin/irb:11:in `<main>'
9: from (irb):5
8: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver.rb:86:in `for'
7: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/driver.rb:44:in `for'
6: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/driver.rb:44:in `new'
5: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/chrome/driver.rb:44:in `initialize'
4: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:69:in `start'
3: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/socket_lock.rb:39:in `locked'
2: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:72:in `block in start'
1: from /home/weefee/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:142:in `connect_until_stable'
Selenium::WebDriver::Error::WebDriverError (unable to connect to chromedriver 127.0.1.1:9515)
Any idea on how to fix it? A few notes:
Some people seem to encounter issues with rbenv
and the shim that it sets up. I'm not using rbenv
at all, so that's irrelevant here.
The above works when I try it on my OSx laptop. Of course there I can easily install chromedriver with brew install chromedriver
and it just seems to work
Alternatively, I uninstalled chromedriver and re-installed it using the chromedriver-helper gem. Still the same result.
I've been tearing my hair out for a while on this - any help would be appreciated. Thanks!
UPDATE
I dug deeper into the source of the selenium-webdriver
gem to see exactly what it was doing when trying to connect to the chromedriver
process.
I was able to replicate the following in my ruby console on the server using the same commands the selenium-webdriver
gem uses:
#
# Start the Chromedriver Process
#
require 'childprocess'
process = ChildProcess.build(*["/usr/local/bin/chromedriver", "--port=9515"])
process.leader = true
process.alive? #=> false
process.start
process.alive? #=> true
#
# Create a Socket connection to 127.0.0.1:9515
#
require 'socket'
require 'selenium-webdriver'
host = Selenium::WebDriver::Platform.localhost #=> "127.0.1.1"
port = Integer(Selenium::WebDriver::Chrome::Service::DEFAULT_PORT) #=> 9515
timeout = 5
# Create and connect to socket
addr = Socket.getaddrinfo(host, port, Socket::AF_INET, Socket::SOCK_STREAM)
sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
sockaddr = Socket.pack_sockaddr_in(port, addr[0][3])
# First need to rescue the writable error and then connect again
# to get the actual error. No idea why but even the official
# rubydocs use this pattern: https://apidock.com/ruby/Socket/connect_nonblock
begin
sock.connect_nonblock(sockaddr)
rescue IO::WaitWritable
IO.select(nil, [sock], nil, 5)
sock.connect_nonblock(sockaddr)
end
#=> Errno::ECONNREFUSED (Connection refused - connect(2) for 127.0.1.1:9515)
So it seems the core error is that the socket refuses to connect on that (local) address and port, even though chromedriver
is very much running on that port.
I don't know much about sockets at all - is this a common error?
Thanks!
I have tried it on the Ubuntu 16.04 (updated) and ruby which comes with the system ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]
.
From what I guess is that you are using wrong, old (in the guide there is version 2.26 which will not work with current chrome), chromedriver, which is not compatible with the current stable chrome.
The current stable chrome is Unpacking google-chrome-stable (71.0.3578.98-1) ...
and so you need to get chromedriver as close as possible to the chrome version.
To get complete list of chromedrivers' versions click here.
In my case that would be a 71.0.3578.80
version:
wget -N http://chromedriver.storage.googleapis.com/71.0.3578.80/chromedriver_linux64.zip
You can then continue as shown at the instructions.
Then you will get working selenium-webdriver
:
irb
irb(main):001:0> require 'selenium-webdriver'
=> true
irb(main):003:0> options = Selenium::WebDriver::Chrome::Options.new
=> #<Selenium::WebDriver::Chrome::Options:0x00000002ee6db0 @args=#<Set: {}>, @binary=nil, @prefs={}, @extensions=[], @options={}, @emulation={}, @encoded_extensions=[]>
irb(main):004:0> options.add_argument('--headless')
=> #<Set: {"--headless"}>
irb(main):005:0> @driver = Selenium::WebDriver.for(:chrome, options: options)
=> #<Selenium::WebDriver::Chrome::Driver:0x..f95c429ee62a3a152 browser=:chrome>
Note: If you have issues installing ffi
install libffi-dev
via apt-get
.
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb stable main" >> /etc/apt/sources.list.d/google.list'
sudo apt-get update
sudo apt-get --only-upgrade install google-chrome-stable
This will keep your version up to date and available. I have had this solve similar problems that were encountered during CI testing. You should be able to setup your @driver
afterwards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With