PyCharm unable to connect to GhostDriver

允我心安 提交于 2019-12-21 03:15:20

问题


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()

The exception I get when running from PyCharm is

WebDriverException: Message: 'Can not connect to GhostDriver' 

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)

To quickly recap:

  • Test passes with python manage.py test
  • Test passes in PyCharm if port is specified manually
  • Test fails in PyCharm if port=free_port()

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 

回答1:


Something in your terminal setup probably influences the networking.

Try to start PyCharm from your terminal:

open -a /Applications/PyCharm.app/

Then run the test again, and it should pass.


Source: sum-up of @CrazyCoder's comments




回答2:


Try installing phantomjs with npm: https://www.npmjs.com/package/phantomjs. I use Mac OS X and I had a similar issue using by selenium and django.



来源:https://stackoverflow.com/questions/17827448/pycharm-unable-to-connect-to-ghostdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!