Home > Software engineering >  Problem running Selenium 4 code in PyCharm
Problem running Selenium 4 code in PyCharm

Time:07-08

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

CodePudding user response:

I'm pretty sure this happens because PyCharm automatically creates and enters a virtual environment, where you probably didn't run a pip install. To make sure, try launching these commands from vscode:

source venv/bin/activate
python your_script.py
deactivate # after executing your script, to exit the venv

If you do get the same error, then you didn't set up a venv properly

  • Related