代码之家  ›  专栏  ›  技术社区  ›  user2490003

` Selenium WebDriver的Ruby Gem无法与Ubuntu 14.04上的Chromedriver连接

  •  7
  • user2490003  · 技术社区  · 6 年前

    我运行的是Ubuntu16.04,我正试图在Ruby中运行一个无头的Chrome浏览器。 chromedriver .

    我已经安装好了 铬合金驱动器 关于Ubuntu的使用 these instructions 然后我通过红宝石运行这个 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)
    

    你知道怎么修吗?几点注意事项

    1. 有些人似乎遇到了一些问题 rbenv 以及它设置的垫片。我不在用 伦贝夫 总之,这在这里无关紧要。

    2. 当我在我的OSX笔记本电脑上尝试时,上面的方法是有效的。当然,我可以很容易地安装Chromedriver brew install chromedriver 它看起来很管用

    3. 或者,我卸载chromedriver并使用 chromedriver-helper 宝石。结果还是一样。

    在这个问题上,我已经把头发扯掉了一段时间了——任何帮助都会被感激的。谢谢!

    更新

    我深入挖掘了 selenium-webdriver 当尝试连接到 铬合金驱动器 过程。

    我可以使用相同的命令在服务器上的Ruby控制台中复制以下内容 Selenium Web驱动程序 创业板用途:

    #
    # 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)
    

    因此,核心错误似乎是套接字拒绝连接到该(本地)地址和端口,即使 铬合金驱动器 在那个端口上运行的非常多。

    我对插座一点都不了解-这是一个常见的错误吗?

    谢谢!

    4 回复  |  直到 6 年前
        1
  •  3
  •   tukan    6 年前

    我在Ubuntu16.04(更新版)和系统附带的Ruby上试用过。 ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu] .

    据我猜测,你使用的是错误的,旧的(在指南中有版本 二点二六 不适用于当前的铬),Chromedriver,即 不兼容 电流稳定铬 .

    目前稳定的铬是 Unpacking google-chrome-stable (71.0.3578.98-1) ... 所以你需要让Chromedriver尽可能接近Chrome版本。

    要获得Chromedrivers版本的完整列表,请单击 here .

    在我的情况下,那将是一个 71.0.3578.80 版本:

    wget -N http://chromedriver.storage.googleapis.com/71.0.3578.80/chromedriver_linux64.zip

    然后您可以按照说明继续。

    那你就开始工作了 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>
    

    注意:如果安装时遇到问题 ffi 安装 libffi-dev 通过 apt-get .

        2
  •  2
  •   Tom    6 年前
    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
    

    这将使您的版本保持最新和可用。我已经解决了CI测试中遇到的类似问题。您应该能够设置 @driver 之后。

        3
  •  -1
  •   Allen    6 年前

    从这条线:

    Selenium::WebDriver::Error::WebDriverError (unable to connect to chromedriver 127.0.1.1:9515)
    

    它建议您尝试连接到127.0.1.1,而您的本地主机应该是127.0.0.1,您可以检查配置吗?

        4
  •  -2
  •   user2490003    6 年前

    所有提供的解决方案都是很好的建议,但最终图坎的回答引导我走上了仔细检查版本的道路。

    我在Ubuntu14.04上没有使用这个设置,它在写这篇文章的时候已经5岁了。 我在Ubuntu16.04机器上重新构建了整个系统,它运行得很好。

    我不知道 什么 导致错误,但似乎是操作系统级别的错误。