使用 webdriver(selenium)的 Python 脚本永远挂起

使用 webdriver(selenium)的 Python 脚本永远挂起

我在专用服务器上运行 Ubuntu Xenial 16.04,我想开始用 Python 编码,因此我遵循了本教程:https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-programming-environment-on-an-ubuntu-16-04-server

我也安装了 Selenium 和 Chromedriver,并编写了这个非常简单的 script.py:

from time import sleep
from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://www.google.com')
sleep(5)
browser.close()

但是当我运行脚本时,它一直挂起,迫使我中断它。当我这样做时,我得到了以下结果:

^CTraceback (most recent call last):
  File "script.py", line 4, in <module>
    browser = webdriver.Chrome()
  File "/home/vcluzeau/python/my_env/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
  File "/home/vcluzeau/python/my_env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/home/vcluzeau/python/my_env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/vcluzeau/python/my_env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 310, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/home/vcluzeau/python/my_env/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 466, in execute
    return self._request(command_info[0], url, body=data)
  File "/home/vcluzeau/python/my_env/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 490, in _request
    resp = self._conn.getresponse()
  File "/usr/lib/python3.5/http/client.py", line 1197, in getresponse
    response.begin()
  File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.5/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.5/socket.py", line 575, in readinto
    return self._sock.recv_into(b)
KeyboardInterrupt

你们知道我该如何解决这个问题吗?谢谢!

答案1

Python 错误滚滚而来包裹来自Python 软件使用动态编译、抽象语法树重写、实时调用堆栈修改和喜欢的组合来摆脱所有使编程变得如此困难的烦人错误。对于导致错误的模块,这是一种即时但临时的解决方案。

相关内容