在Pycharm中运行Selenium 4代码的问题
我正在尝试在Pycharm中运行一个非常简单的Selenium Python代码,但是每当我调用driver = webdriver.firefiver.firefox(driver_path)
时,每次都会出现以下错误。
/home/kimilao/PycharmProjects/pythonOne/main.py:7: DeprecationWarning: firefox_profile has been deprecated, please pass in an Options object
driver = webdriver.Firefox(driver_path)
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 169, in _new_conn
conn = connection.create_connection(
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 96, in create_connection
raise err
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 86, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 394, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "/usr/lib/python3.10/http/client.py", line 1282, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.10/http/client.py", line 1328, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.10/http/client.py", line 1277, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.10/http/client.py", line 1037, in _send_output
self.send(msg)
File "/usr/lib/python3.10/http/client.py", line 975, in send
self.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 200, in connect
conn = self._new_conn()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 181, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fedb7be9c00>: Failed to establish a new connection: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/kimilao/PycharmProjects/pythonOne/main.py", line 7, in <module>
driver = webdriver.Firefox(driver_path)
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 177, in __init__
super().__init__(
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 277, in __init__
self.start_session(capabilities, browser_profile)
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 370, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 433, in execute
response = self.command_executor.execute(driver_command, params)
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/remote/remote_connection.py", line 344, in execute
return self._request(command_info[0], url, body=data)
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/remote/remote_connection.py", line 366, in _request
response = self._conn.request(method, url, body=body, headers=headers)
File "/usr/lib/python3/dist-packages/urllib3/request.py", line 78, in request
return self.request_encode_body(
File "/usr/lib/python3/dist-packages/urllib3/request.py", line 170, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/usr/lib/python3/dist-packages/urllib3/poolmanager.py", line 375, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 783, in urlopen
return self.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 783, in urlopen
return self.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 783, in urlopen
return self.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=57917): Max retries exceeded with url: /session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fedb7be9c00>: Failed to establish a new connection: [Errno 111] Connection refused'))
这是我的完整代码:
#!/usr/bin/python3
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
driver_path = "/home/kimilao/Downloads/geckodriver-v0.31.0-linux64"
driver = webdriver.Firefox(driver_path)
driver.get("https://www.python.org/")
print(driver.title)
driver.quit()
问题在于,我可以在使用崇高的文本,VSCODE和终端而不是Pycharm时运行此代码,而无需错误,我想知道为什么这会发生在我身上。谢谢!
- Python: 3.10.4
- Selenium: 4.3.0
- Firefox: 102.0 (64-bit)
- Gecko Driver: geckodriver-v0.31.0-linux64
I am trying to run a very simple selenium python code in PyCharm, but it throws out the following error every time when I call driver = webdriver.Firefox(driver_path)
.
/home/kimilao/PycharmProjects/pythonOne/main.py:7: DeprecationWarning: firefox_profile has been deprecated, please pass in an Options object
driver = webdriver.Firefox(driver_path)
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 169, in _new_conn
conn = connection.create_connection(
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 96, in create_connection
raise err
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 86, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 394, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "/usr/lib/python3.10/http/client.py", line 1282, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.10/http/client.py", line 1328, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.10/http/client.py", line 1277, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.10/http/client.py", line 1037, in _send_output
self.send(msg)
File "/usr/lib/python3.10/http/client.py", line 975, in send
self.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 200, in connect
conn = self._new_conn()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 181, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fedb7be9c00>: Failed to establish a new connection: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/kimilao/PycharmProjects/pythonOne/main.py", line 7, in <module>
driver = webdriver.Firefox(driver_path)
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 177, in __init__
super().__init__(
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 277, in __init__
self.start_session(capabilities, browser_profile)
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 370, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 433, in execute
response = self.command_executor.execute(driver_command, params)
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/remote/remote_connection.py", line 344, in execute
return self._request(command_info[0], url, body=data)
File "/home/kimilao/.local/lib/python3.10/site-packages/selenium/webdriver/remote/remote_connection.py", line 366, in _request
response = self._conn.request(method, url, body=body, headers=headers)
File "/usr/lib/python3/dist-packages/urllib3/request.py", line 78, in request
return self.request_encode_body(
File "/usr/lib/python3/dist-packages/urllib3/request.py", line 170, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/usr/lib/python3/dist-packages/urllib3/poolmanager.py", line 375, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 783, in urlopen
return self.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 783, in urlopen
return self.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 783, in urlopen
return self.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=57917): Max retries exceeded with url: /session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fedb7be9c00>: Failed to establish a new connection: [Errno 111] Connection refused'))
Here's my full code:
#!/usr/bin/python3
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
driver_path = "/home/kimilao/Downloads/geckodriver-v0.31.0-linux64"
driver = webdriver.Firefox(driver_path)
driver.get("https://www.python.org/")
print(driver.title)
driver.quit()
The problem is that I can run this code without the errors when using Sublime Text, vscode and terminal, but not PyCharm, and I wonder why this happens to me. Thanks!
- Python: 3.10.4
- Selenium: 4.3.0
- Firefox: 102.0 (64-bit)
- Gecko Driver: geckodriver-v0.31.0-linux64
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定这是因为Pycharm会自动创建并输入
虚拟环境
,您可能没有运行pip install
。为了确保,请尝试从VSCODE启动这些命令:如果您确实遇到相同的错误,则未正确设置
venv
I'm pretty sure this happens because PyCharm automatically creates and enters a
virtual environment
, where you probably didn't run apip install
. To make sure, try launching these commands from vscode:If you do get the same error, then you didn't set up a
venv
properly我使用pycharm(尽管在Windows上),并且我的导入语句和驱动程序对象完全不同。也许您可以尝试一下,看看它是否对您有用?
注意:这是在下载和安装的驱动程序的假设下(另请确认您的互联网连接。我使用Chrome经历了类似的东西,但我的问题是Internet连接)
i use Pycharm (albeit on windows) and my import statements and driver object are completely different. Maybe you can give it a try and see if it works for you?
Note: this is under the assumption that the driver as been downloaded and installed (also, confirm your internet connection. I experienced something similar using chrome, but my problem was the internet connection)