【发布时间】:2019-06-12 08:19:31
【问题描述】:
我正在运行 Ubuntu 16.04,并且正在尝试使用 chromedriver 在 ruby 中运行无头 Chrome 浏览器。
我已经在 Ubuntu 上使用 these instructions 安装了 chromedriver,然后我通过 ruby irb 控制台运行它:
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)
关于如何解决它的任何想法?几点说明:
有些人似乎遇到了
rbenv及其设置的垫片的问题。我根本没有使用rbenv,所以这里无关紧要。当我在我的 OSx 笔记本电脑上尝试时,上述方法有效。当然,我可以使用
brew install chromedriver轻松安装 chromedriver,而且它似乎可以正常工作或者,我卸载了 chromedriver 并使用 chromedriver-helper gem 重新安装了它。结果还是一样。
我已经为此烦恼了一段时间 - 任何帮助都将不胜感激。谢谢!
更新
我深入挖掘了selenium-webdriver gem 的源代码,以查看它在尝试连接到chromedriver 进程时到底做了什么。
我能够使用selenium-webdriver gem 使用的相同命令在服务器上的 ruby 控制台中复制以下内容:
#
# 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)
所以看起来核心错误是套接字拒绝连接该(本地)地址和端口,即使chromedriver 在该端口上运行得非常频繁。
我对套接字一无所知 - 这是一个常见错误吗?
谢谢!
【问题讨论】:
-
它对我有用。我不知道你的情况是什么问题。顺便说一句,使用 WATIR,它是 Ruby Selenium Binding 的包装器。
-
我的一个朋友遇到了同样的问题,升级了chrome版本并解决了!试一试
标签: ruby selenium selenium-webdriver selenium-chromedriver