PyCharm无法连接到GhostDriver

问题描述 投票:14回答:1

当我通过python manage.py test运行它时,我有一个单元测试通过,但是当我从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运行时遇到的异常是

WebDriverException: Message: 'Can not connect to GhostDriver' 

我花了相当多的时间来研究这个问题,我注意到当我手动指定端口时,测试在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中失败

PyCharm做的是让测试无法连接到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 
python unit-testing selenium-webdriver phantomjs pycharm
1个回答
0
投票

尝试使用npm:https://www.npmjs.com/package/phantomjs安装phantomjs。我使用Mac OS X,我有一个类似的问题使用selenium和django。

© www.soinside.com 2019 - 2024. All rights reserved.