selenium.common.exceptions.WebDriverException:消息:“无法连接到GhostDriver" [英] selenium.common.exceptions.WebDriverException: Message: 'Can not connect to GhostDriver'

查看:1243
本文介绍了selenium.common.exceptions.WebDriverException:消息:“无法连接到GhostDriver"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Centos服务器上的selenium.webdriver内部运行PhantomJS. PhantomJS在路径中,并且可以从终端正常运行.但是,在脚本中,它似乎已启动,但是此后无法在指定的端口上到达(我尝试从提供程序29842和60099尝试使用2个不同的打开的端口,它们都无法正常工作,也没有在没有指定端口的情况下启动它). >

此错误发生在selenium.webdriver.common.utils中:

try:
    socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_.settimeout(1)
    socket_.connect(("localhost", port))
    socket_.close()
    return True
except socket.error:
    return False

这是从我的脚本中获得的(我尝试不使用任何参数,也没有写完整的可执行文件路径,但是都无效):

self.browser = webdriver.PhantomJS(
            port=29842,
            desired_capabilities={
                'javascriptEnabled': True,
                'platform': 'windows',
                'browserName': 'Mozilla',
                'version': '5.0',
                'phantomjs.page.settings.userAgent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36"
            }
        )

这是从selenium.webdriver.phantomjs.service初始化webdriver的脚本.我检查并subprocess.Popen实际上启动了phantomjs,该错误发生在while循环中:

    try:
        self.process = subprocess.Popen(self.service_args,
                                        stdout=self._log, stderr=self._log)

    except Exception as e:
        raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)

    count = 0
    while not utils.is_connectable(self.port):
        print utils.is_connectable(self.port)
        count += 1
        time.sleep(1)
        if count == 30:
             raise WebDriverException("Can not connect to GhostDriver")

所有软件包均为最新版本:python 2.7,selenium 2和phantomjs 1.9二进制文件,集成了ghostdriver.我在Ubuntu本地计算机上正确执行了相同的脚本,执行与服务器上完全相同的操作.服务器上有什么区别?

解决方案

升级到新版本后,我在Ubuntu上遇到了此问题.我重新安装了所有的nodejs和python软件包,但是我认为解决了该问题的是确保将nodejs可执行文件符号链接到node.

这些是我使用的命令:

apt-get remove node nodejs
apt-get install build-essential python-dev phantomjs npm nodejs
ln -s /usr/bin/nodejs /usr/bin/node
npm install -g phantomjs
pip install selenium bson BeautifulSoup pymongo

I'm trying to run PhantomJS from within selenium.webdriver on a Centos server. PhantomJS is in the path and is running properly from terminal. However in the script it appears to be launched, but afterwards cannot be reached on the specified port (I tried 2 different opened ports from my provider 29842 and 60099, they both are not working and neither launching it without a specified port).

The error happens here in selenium.webdriver.common.utils:

try:
    socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_.settimeout(1)
    socket_.connect(("localhost", port))
    socket_.close()
    return True
except socket.error:
    return False

This is from my script (I tried without any parameters as well as writing the complete path to the executable and neither worked):

self.browser = webdriver.PhantomJS(
            port=29842,
            desired_capabilities={
                'javascriptEnabled': True,
                'platform': 'windows',
                'browserName': 'Mozilla',
                'version': '5.0',
                'phantomjs.page.settings.userAgent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36"
            }
        )

And this the script that initialises the webdriver from selenium.webdriver.phantomjs.service. I checked and subprocess.Popen actually lauches phantomjs, the error happens in the while loop:

    try:
        self.process = subprocess.Popen(self.service_args,
                                        stdout=self._log, stderr=self._log)

    except Exception as e:
        raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)

    count = 0
    while not utils.is_connectable(self.port):
        print utils.is_connectable(self.port)
        count += 1
        time.sleep(1)
        if count == 30:
             raise WebDriverException("Can not connect to GhostDriver")

All the packages are the latest version: python 2.7, selenium 2 and phantomjs 1.9 binary with ghostdriver integrated. I made the same script work properly on my Ubuntu local machine, doing exactly the same things I did on the server. What is different on the server?

解决方案

I had this issue on Ubuntu after upgrading to a new version. I re-installed all of the nodejs and python packages, but what I think solved the issue was making sure the nodejs executable was symbolically linked to node.

These are the commands I used:

apt-get remove node nodejs
apt-get install build-essential python-dev phantomjs npm nodejs
ln -s /usr/bin/nodejs /usr/bin/node
npm install -g phantomjs
pip install selenium bson BeautifulSoup pymongo

这篇关于selenium.common.exceptions.WebDriverException:消息:“无法连接到GhostDriver"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆