该解决方案已经在Ubuntu中验证成功!
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")
.
To fix:
- Open a port.
sudo iptables -A INPUT -s 127.0.0.1 -p tcp --dport 65000 -j ACCEPT
- Create a PhantomJS driver that uses that port
driver = webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs', port=65000)
但以上解决方法不足以解决Mac出现的问题,即:Cannot connect the service /usr/local/bin/phantomjs.
那么如何解决呢?
经过花费近四五个小时的时间进行搜索、尝试、在搜索、再尝试。。。。。,最终确定问题也许并非出在phantomjs上,而是selenium。使用 pip check selenium
查到缺少一个必需的模块 tornado
,于是 pip install tornado
,再次尝试,问题解决。