Question: Why do I get the error I get, although to me it seems that the webdriver is in PATH?
Problem description and context:
I am trying to do some coding that requires using the webdriver manager for Chrome. I am testing the configuration with the following code (not my own, should work as it is, as it's provided in course material).
import django, selenium, bs4, requests from selenium import webdriver from selenium.webdriver.chrome.options import Options import socket # There is a 'feature' in selenium which may cause the firewall to react unless you provide an explicit port for the driver def free_port(): free_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) free_socket.bind(('localhost', 0)) free_socket.listen(5) port = free_socket.getsockname()[1] free_socket.close() return port options = Options() options.headless = True options.add_argument('--no-sandbox') driver = webdriver.Chrome(port=free_port(), options=options) driver.get('https://www.google.com') print('Good so far') However, when running the above script in Spyder, I get the following error.
runfile('/Users/USERNAME/Downloads/sanity.py', wdir='/Users/USERNAME/Downloads') Traceback (most recent call last): File "/Users/USERNAME/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 72, in start self.process = subprocess.Popen(cmd, env=self.env, File "/Users/USERNAME/opt/anaconda3/lib/python3.8/subprocess.py", line 854, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/Users/USERNAME/opt/anaconda3/lib/python3.8/subprocess.py", line 1702, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/USERNAME/Downloads/sanity.py", line 19, in <module> driver = webdriver.Chrome(port=free_port(), options=options) File "/Users/USERNAME/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__ self.service.start() File "/Users/USERNAME/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 81, in start raise WebDriverException( WebDriverException: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home When echoing PATH in terminal, I see there the folder /Users/USERNAME/opt/anaconda/pkgs, where both the webdriver-manager and selenium are located.
options.headless = Trueis not how you enabled headless mode.echo $PATHin terminal. So I added both/Users/USERNAME/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.pyand/Users/USERNAME/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/common/service.pytoos.sys.path. However, I still get the error thatWebDriverException: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home.