gpt4 book ai didi

python - selenium.common.exceptions.WebDriverException : Message: 'Can not connect to GhostDriver'

转载 作者:IT老高 更新时间:2023-10-28 21:12:57 25 4
gpt4 key购买 nike

我正在尝试在 Centos 服务器上的 selenium.webdriver 中运行 PhantomJS。 PhantomJS 在路径中并且从终端正常运行。但是在脚本中它似乎已启动,但之后无法在指定的端口上访问(我尝试了来 self 的提供商 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 的脚本(我尝试不带任何参数以及编写可执行文件的完整路径但均未成功):

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 实际上 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")

所有软件包都是最新版本:python 2.7、selenium 2 和 phantomjs 1.9 二进制文件,集成了 ghostdriver。我让相同的脚本在我的 Ubuntu 本地机器上正常工作,做的事情与我在服务器上做的完全一样。服务器上有什么不同?

最佳答案

升级到新版本后,我在 Ubuntu 上遇到了这个问题。我重新安装了所有的 nodejs 和 python 包,但我认为解决问题的是确保 nodejs 可执行文件符号链接(symbolic link)到 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

关于python - selenium.common.exceptions.WebDriverException : Message: 'Can not connect to GhostDriver' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18916123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com