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.
from this line:
Selenium::WebDriver::Error::WebDriverError (unable to connect to chromedriver 127.0.1.1:9515)
it suggests you are trying to connect to 127.0.1.1, while your localhost should be 127.0.0.1, could you check your configuration?
All the provided solutions were great recommendations, but ultimately tukan's answer led me down the path of carefully checking versions.
The setup I had didn't work on Ubuntu 14.04, which is about 5 years old at the time of this writing. I re-built the whole thing on an Ubuntu 16.04 machine and it worked perfectly.
I don't know what causes the error, but it seems to be something at the operating system level.
Related
I'm working on my project, where i need to press a button by Selenium. But when i start it, it gives me this error
/home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/service_manager.rb:139:in `connect_until_stable': unable to connect to /usr/bin/chromedriver 127.0.0.1:9515 (Selenium::WebDriver::Error::WebDriverError)
from /home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/service_manager.rb:57:in `block in start'
from /home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/socket_lock.rb:41:in `locked'
from /home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/service_manager.rb:54:in `start'
from /home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/service.rb:84:in `launch'
from /home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/driver.rb:374:in `service_url'
from /home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/driver.rb:311:in `create_bridge'
from /home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/driver.rb:74:in `initialize'
from /home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/driver.rb:47:in `new'
from /home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/driver.rb:47:in `for'
from /home/yarikhrom/.rvm/gems/ruby-3.0.2/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver.rb:88:in `for'
from test.rb:3:in `<main>'
Code:
require 'selenium-webdriver'
driver = Selenium::WebDriver.for:chrome
driver.get "https://www.google.com/"
driver.find_element(:xpath, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[2]').click
How can i fix this? I've found that there is may be some problems with google dependencies, but I clearly don't understand how to change it
Two points from my side.
First off, check whether chromedriver.exe exists in c:\ruby30\bin If you use other version of ruby, then you would be finding ruby accordingly like ruby26 ruby 27 etc.
You look like you are using Linux operating system, eh? If so, check whether chromedriver.exe is on right path.
Second off,
driver.find_element(:xpath, '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[2]').click
this one is the old style, it is not working now.
Use this code
driver.find_element(xpath: '/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[2]').click
I had a similar error when setting up WSL 2. Updating to the newest version of WSL by running wsl --update in powershell fixed the issue.
I am using Linux Chrome and Chromedriver, not Windows. My distro is Ubuntu 20.04. To install these I did the following:
Installing Chrome
sudo apt install fonts-liberation libappindicator3-1 libasound2 libgbm1 libnspr4 libnss3 libxss1 xdg-utils
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
echo 'alias chrome="google-chrome-stable"' >> ~/.bashrc
exec $SHELL
Installing Chromedriver
sudo apt-get update
sudo apt-get install chromium-chromedriver
I just finished setting up another development server for my API built using Ruby and Sinatra, however on this server I can't get the curl gem to work properly.
I've installed libcurl and libcurl-devel, and installed the curl gem without any errors, but when I try to use it in code, it always fails. Below is an example in irb:
irb(main):001:0> require 'curl'
=> true
irb(main):002:0> http = Curl.get("http://www.mysuperawesomeapi.com/someendpoint") do|http|
irb(main):003:1* http.headers['accept'] = 'application/JSON'
irb(main):004:1> end
NameError: uninitialized constant Curl
from (irb):2
from /usr/bin/irb:11:in `<main>'
The difference between this development server, and the other one is that this one is using Fedora 21 32bit (hardware limitation) while the other is using CentOS 7 64bit and is a virtual machine. When I try the same code above on irb on the CentOS VM, it works as expected. Any insight would be greatly appreciated.
It looks like in curl (unlike as in curb), there is a class CURL, but not Curl.
Inside gemfile include:
gem 'curl'
gem 'curb'
I am trying to get cucumber tests to run on our headless centOS box.
I have installed Xvfb, firefox, and my test suite, which functions on our non-headless(headed?) machines.
Versions
CentOS: 6.2
firefox: 23.0.1
headless: 1.0.1
selenium-webdriver: 2.35.0
watir-webdriver: 0.6.4
ruby: 1.9.3
In irb:
1.9.3-p448 :001 > require 'watir-webdriver'
=> true
1.9.3-p448 :002 > require 'headless'
=> true
1.9.3-p448 :004 > headless = Headless.new
=> #<Headless:0x000000025e0860 #display=99, #autopick_display=true, #reuse_display=true, #dimensions="1280x1024x24", #video_capture_options={}, #destroy_at_exit=true>
1.9.3-p448 :005 > headless.start
=> #<Proc:0x000000025e5180#/usr/local/rvm/gems/ruby-1.9.3-p448/gems/headless-1.0.1/lib/headless.rb:175>
1.9.3-p448 :006 > b = Watir::Browser.new(:firefox)
Selenium::WebDriver::Error::WebDriverError: unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/firefox/launcher.rb:79:in `connect_until_stable'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/firefox/launcher.rb:37:in `block in launch'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/firefox/socket_lock.rb:20:in `locked'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/firefox/launcher.rb:32:in `launch'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/firefox/bridge.rb:24:in `initialize'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/common/driver.rb:31:in `new'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/common/driver.rb:31:in `for'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver.rb:67:in `for'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/watir-webdriver-0.6.4/lib/watir-webdriver/browser.rb:46:in `initialize'
from (irb):6:in `new'
from (irb):6
from /usr/local/rvm/rubies/ruby-1.9.3-p448/bin/irb:13:in `<main>'
I have the same issue when trying to run the tests (this is just easier to reproduce).
I checked out the other questions here, but I am already using the latest version. According to the changelog they support firefox 23. Does anyone have any ideas? Thanks in advance!
UPDATE 8/28/2013 0900:
I was getting this error when I tried to run Xvfb.
[dix] Could not init font path element catalogue:/etc/X11/fontpath.d, removing from list!
[dix] Could not init font path element built-ins, removing from list!
I fixed it by using
yum -y install libXfont
But I am still getting the same error.
UPDATE 8/28/2013 0930:
As per TDHM's suggestion, I downgraded firefox to 17.0.8 by running
yum downgrade firefox
But I am still getting the same error.
A co-worker of mine managed to fix the issue.
Run this line of code:
$ dbus-uuidgen > /var/lib/dbus/machine-id
And the problem is fixed. This is the source for the fix
I'm not sure, but see if downgrading Firefox version works. Because many times latest version of Selenium has problems with latest browser versions.
We've been running integration tests successfully against Rails 2 using Selenium on both chrome and firefox. However, we've recently upgraded to Rails 3 and are running into issues creating a chrome webdriver instance.
When we attempt to create, we get the following stack:
irb(main):002:0> profile = Selenium::WebDriver::Chrome::Profile.new
translate])#<Selenium::WebDriver::Chrome::Profile:0x64f2fd0 #extensions=[], #model=nil>
irb(main):003:0> profile['download.prompt_for_download'] = false
false
irb(main):004:0> driver = Selenium::WebDriver.for(:chrome, :profile => profile, :switches => %w[--ignore-certificate-errors --disable-popup-blocking --disable-translate])
ArgumentError: wrong number of arguments (0 for 1)
from /var/www/myapp/shared/bundle/ruby/1.8/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/common/platform.rb:157:in `open'
from /var/www/myapp/shared/bundle/ruby/1.8/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/common/platform.rb:157:in `ip'
from /var/www/myapp/shared/bundle/ruby/1.8/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/common/platform.rb:170:in `interfaces'
from /var/www/myapp/shared/bundle/ruby/1.8/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/common/port_prober.rb:23:in `free?'
from /var/www/myapp/shared/bundle/ruby/1.8/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/common/port_prober.rb:5:in `above'
from /var/www/myapp/shared/bundle/ruby/1.8/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/chrome/service.rb:33:in `default_service'
from /var/www/myapp/shared/bundle/ruby/1.8/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/chrome/bridge.rb:14:in `initialize'
from /var/www/myapp/shared/bundle/ruby/1.8/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/common/driver.rb:37:in `new'
from /var/www/myapp/shared/bundle/ruby/1.8/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/common/driver.rb:37:in `for'
from /var/www/myapp/shared/bundle/ruby/1.8/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver.rb:67:in `for'
from (irb):4
Any tips?
I saw this when I had included a gem which included the "backports" gem as part of its dependencies into my Gemfile. The backports gem rewrites some Ruby 1.8 internals, which overwrote parts of the UDPSocket core class. Try getting rid of the backports gem and giving it a shot.
ChromeDriver Installation:
Download Chromedriver 2.x from "http://chromedriver.storage.googleapis.com/index.html?path=2.8/." Unzip it and save it in a folder on any drive. Set path by following steps :
Copy the path till chromedriver.exe
Right click on Computer and select ‘Properties’
Select ‘Advanced system variables’
Select ‘Environment variables’
Click on Edit button for ‘Path’ variable of ‘User variables’
Append the chromedriver path
Save changes.
Run Selenium Test :
Now run your selenium test. It will run on the chrome browser.
Sample Code
require 'selenium-webdriver'
$driver = Selenium::WebDriver.for :chrome
$driver.navigate.to "https://www.google.co.in/"
$driver.manage().window().maximize()
$driver.quit()
NOTE : You need to install latest chromedriver for latest version of Chrome browser
This was caused by an interference with the backports gem and the fact that the socket library reuses IO.open although it changes the interface.
Upgrading backports to v2.6.7 or above should resolve this.
We're trying to get rails to talk to a sqlserver db on Azure, we install freeTDS with openssl and libiconv:
./configure --prefix=/usr/local --with-libiconv-prefix=DIR --with-openssl=DIR
make
make install
We then add tiny_TDS and activerecord-sqlserver-adapter to the gemfile:
gem 'tiny_tds'
gem 'activerecord-sqlserver-adapter'
$ bundle install
Configure the database:
development:
adapter: sqlserver
host: xxxxxxx.database.windows.net
mode: DBLIB
port: 1433
database: xxxxx
username: xxxxxxxx
password: x
azure: true
Run the server:
$ rails s
Everything works great up until this point, but as soon as you visit the site ruby crashes.
steven#jenny:~/irr$ rails server -p 3001
=> Booting WEBrick
=> Rails 3.0.4 application starting in development on http://0.0.0.0:3001
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-07-24 15:37:00] INFO WEBrick 1.3.1
[2012-07-24 15:37:00] INFO ruby 1.9.2 (2012-02-14) [x86_64-linux]
[2012-07-24 15:37:00] INFO WEBrick::HTTPServer#start: pid=7586 port=3001
/home/steven/.rvm/gems/ruby-1.9.2-p318/gems/tiny_tds-0.5.1/lib/tiny_tds/client.rb:68: [BUG] Segmentation fault
ruby 1.9.2p318 (2012-02-14 revision 34678) [x86_64-linux]
-- control frame ----------
c:0048 p:---- s:0232 b:0232 l:000231 d:000231 CFUNC :connect
c:0047 p:0429 s:0228 b:0228 l:000227 d:000227 METHOD /home/steven/.rvm/gems/ruby-1.9.2-p318/gems/tiny_tds-0.5.1/lib/tiny_tds/client.rb:68
We've tried several versions of ruby: 1.9.2-p318, 1.9.2-p320, 1.9.3-p125. Same error with all of them.
This is on ubuntu 11.10 using rvm and FreeTDS-0.9.1.
Any Ideas on a workaround?
Have I made an error somewhere along the way?
Edit
Output from IRB:
require 'tiny_tds'
client = TinyTds::Client.new(:username =>'XXXXX#XXXXXXX.database.windows.net', :password => 'XXXXXXX', :host => 'XXXXXX.database.windows.net', :mode => 'DBLIB', :azure => 'true')
SystemStackError: stack level too deep from /home/martinr/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!!
It seems to be a bug with ruby itself:
/home/martinr/.rvm/gems/ruby-1.9.2-p320/gems/tiny_tds-0.5.1/lib/tiny_tds/client.rb:68: [BUG] Segmentation fault (core dumped)
You should just do a simple IRB prompt and do something like this mentioned on the TinyTDS read me.
require 'tiny_tds'
client = TinyTds::Client.new(...)
Fill in your connection params. Simple stuff like username, password, and host. Let me know how things go.
https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/wiki/Using-Azure
https://github.com/rails-sqlserver/tiny_tds#using-tinytds-with-azure
The error seems to be caused by having a username username#servername
When you remove the #severname part, ruby no longer crashes