简体   繁体   English

selenium.common.exceptions.WebDriverException:消息:'无法连接到GhostDriver'

[英]selenium.common.exceptions.WebDriverException: Message: 'Can not connect to GhostDriver'

I'm trying to run PhantomJS from within selenium.webdriver on a Centos server. 我正在尝试从Centos服务器上的selenium.webdriver运行PhantomJS PhantomJS is in the path and is running properly from terminal. PhantomJS在路径中并且从终端正常运行。 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). 但是在脚本中它似乎已启动,但之后无法在指定的端口上访问(我尝试了2个不同的打开端口,来自我的提供程序29842和60099,它们都不工作,并且没有指定端口也没有启动它)。

The error happens here in selenium.webdriver.common.utils : 错误发生在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 . 这是从selenium.webdriver.phantomjs.service初始化webdriver的脚本。 I checked and subprocess.Popen actually lauches phantomjs, the error happens in the while loop: 我检查和subprocess.Popen实际上lauches 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")

All the packages are the latest version: python 2.7, selenium 2 and phantomjs 1.9 binary with ghostdriver integrated. 所有软件包都是最新版本:python 2.7,selenium 2和phantomjs 1.9二进制文件,集成了ghostdriver。 I made the same script work properly on my Ubuntu local machine, doing exactly the same things I did on the server. 我在Ubuntu本地计算机上使相同的脚本正常工作,与我在服务器上执行的操作完全相同。 What is different on the server? 服务器上有什么不同?

I had this issue on Ubuntu after upgrading to a new version. 升级到新版本后,我在Ubuntu上遇到了这个问题。 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 . 我重新安装了所有的nodejs和python包,但我认为解决了这个问题的方法是确保nodejs可执行文件符号链接到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

在Linux Mint 14上安装nodejs-legacy软件包解决了这个问题。

sudo apt-get install nodejs-legacy

For me, this was a firewall issue. 对我来说,这是一个防火墙问题。 Phantom requires an open port to connect. 幻影需要一个开放的端口来连接。 If the port is blocked by a firewall, you'll get WebDriverException("Can not connect to GhostDriver") . 如果端口被防火墙阻止,您将获得WebDriverException("Can not connect to GhostDriver")

To fix: 修理:

  1. Open a port. 打开一个端口。

sudo iptables -A INPUT -s 127.0.0.1 -p tcp --dport 65000 -j ACCEPT

  1. Create a PhantomJS driver that uses that port 创建使用该端口的PhantomJS驱动程序

driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs', port=65000)

I had this problem and found out what was causing it. 我遇到了这个问题并找出了导致它的原因。 In another tutorial on developing Facebook applications, I was told to nano in to /etc/hosts and change it from this- 在另一个关于开发Facebook应用程序的教程中,我被告知纳入/ etc / hosts并将其更改为 -

127.0.0.1    localhost

to this- 对此 -

127.0.0.1    test1.com

and save the file. 并保存文件。 Incidentally, PhantomJS() was not working anymore after that. 顺便说一句,之后PhantomJS()不再工作了。 I just went back in to the /etc/hosts file and switched it back to localhost 我刚回到/ etc / hosts文件并将其切换回localhost

127.0.0.1    localhost

and it works again. 它再次有效。

I am testing a python/django application using selenium with phantomjs. 我正在测试使用selenium和phantomjs的python / django应用程序。 Everything worked fine during first (and sometimes second test), but once running the next test, selenium printed the exact same error message. 在第一次(有时是第二次测试)期间一切正常,但是一旦运行下一次测试,selenium就会打印完全相同的错误消息。

Furthermore, if I ran phantomjs from console afterwards, I received the following node error: 此外,如果我之后从控制台运行phantomjs,我收到以下节点错误:

child_process.js:1136 var err = this._handle.spawn(options); 
                                             ^

TypeError: Bad argument 
       at TypeError (native)
       at ChildProcess.spawn (child_process.js:1136:26)
       at exports.spawn (child_process.js:995:9)
       at Object.<anonymous> (/usr/local/lib/node_modules/phantomjs/bin/phantomjs:22:10)
       at Module._compile (module.js:460:26)
       at Object.Module._extensions..js (module.js:478:10)
       at Module.load (module.js:355:320)
       at Function.Module._load (module.js:310:12)
       at Function.Module.runMain (module.js:501:10)
       at startup (node.js:129:16)

I tried reinstalling phantomjs via npm (both locally and globally) several times, but the behaviour persisted. 我尝试了几次通过npm(本地和全局)重新安装phantomjs,但行为仍然存在。

Eventually, I uninstalled phantomjs via npm and downloaded the phantomjs bin from phantomjs.org instead. 最后,我通过npm卸载了phantomjs并从phantomjs.org下载了phantomjs bin Put that inside my /usr/local/bin (I am on MacOS) and got rid of the error since! 把它放在我的/ usr / local / bin(我在MacOS上)并且从那以后就摆脱了错误!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 selenium.common.exceptions.WebDriverException: 消息:无法通过 Selenium Python 使用 ChromeDriver Chrome 连接到服务错误 - selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python selenium.common.exceptions.WebDriverException:消息:无法加载配置文件 - selenium.common.exceptions.WebDriverException: Message: Can't load the profile selenium.common.exceptions.WebDriverException:消息:无法连接到服务。/drivers/chromedriver.exe - selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service ./drivers/chromedriver.exe selenium.common.exceptions.WebDriverException:消息:打开chrome浏览器时无法连接到服务chromedriver.exe - selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe while opening chrome browser selenium.common.exceptions.WebDriverException:消息:无法连接到服务 /usr/bin/safaridriver - selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /usr/bin/safaridriver Selenium 因 selenium.common.exceptions.WebDriverException 崩溃:消息:newSession - Selenium crashing with selenium.common.exceptions.WebDriverException: Message: newSession selenium.common.exceptions.WebDriverException:消息:在Selenium Python中退出 - selenium.common.exceptions.WebDriverException: Message: quit in Selenium Python selenium.common.exceptions.WebDriverException:消息:在 Python selenium 4 - selenium.common.exceptions.WebDriverException: Message: in Python selenium 4 selenium.common.exceptions.WebDriverException:消息:未知错误:无法在 localhost:8733 \\\\ 连接到 chrome 怎么回事? - selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at localhost:8733 \\\\ What's going on? selenium.common.exceptions.WebDriverException:消息:会话未创建异常 - selenium.common.exceptions.WebDriverException: Message: session not created exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM