PyCharm无法连接到GhostDriver [英] PyCharm unable to connect to GhostDriver
问题描述
我有一个单元测试,当我通过python manage.py test
运行它时通过了,但是当我从PyCharm内部运行时失败了.
I have a unit test that is passing when I run it via python manage.py test
, but failing when I run it from within PyCharm.
def test_alpha(self):
from selenium.webdriver.common.utils import free_port
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path=PHANTOMJS_PATH, port=free_port())
driver.quit()
从PyCharm运行时遇到的异常是
The exception I get when running from PyCharm is
WebDriverException: Message: 'Can not connect to GhostDriver'
我花了很多时间来研究这个问题,并且我注意到当我手动指定端口时,测试会在PyCharm中通过.
I've spent a fair amount of time digging into this problem, and I've noticed that when I specify a port manually the test passes within PyCharm.
# suppose 50000 happens to be a free port on your computer
driver = webdriver.PhantomJS(executable_path=PHANTOMJS_PATH, port=50000)
要快速回顾一下:
- 通过
python manage.py test
通过测试
- 如果手动指定端口,则测试会通过PyCharm
- 如果
port=free_port()
,则在PyCharm中测试失败
- Test passes with
python manage.py test
- Test passes in PyCharm if port is specified manually
- Test fails in PyCharm if
port=free_port()
PyCharm在做什么,这使得测试无法连接到Ghostdriver?
What is PyCharm doing that is making the test unable to connect to Ghostdriver?
# For convenience, the `free_port()` code snippet is here
# selenium.webdriver.common.utils.freeport
def free_port():
free_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
free_socket.bind(('127.0.0.1', 0))
free_socket.listen(5)
port = free_socket.getsockname()[1]
free_socket.close()
return port
推荐答案
终端设置中的某些内容可能会影响联网.
Something in your terminal setup probably influences the networking.
尝试从您的终端启动PyCharm :
open -a /Applications/PyCharm.app/
然后再次运行测试,它应该通过.
Then run the test again, and it should pass.
来源:@CrazyCoder的评论总结
这篇关于PyCharm无法连接到GhostDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!