1

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.

8
  • Does a simple run without all the options work? Commented Nov 24 at 14:45
  • you are not even using webdriver-manager in your code, and your post title doesn't match the error you posted. The reason you are getting the error is that the version of selenium published by Anaconda doesn't include Selenium Manager. You need to either manually install chromedriver and create a Service object that uses it, use webdriver-manager and create a Service object that uses it, or use the regular selenium package instead of Anaconda's (that will include Selenium Manager). Also, unrelated to your error, but options.headless = True is not how you enabled headless mode. Commented Nov 24 at 16:14
  • PATH env variable would need to be path to the chromedriver executable. (not only the folder) If you use the latest Selenium version it should download executables/manage paths for you. Also, by default Selenium would choose a free port for driver service... not sure it's logic would be much different than yours in "free_port" function. (I'd ditch that unless you see some problems regarding port...) Commented Nov 24 at 19:22
  • Thanks for the comments. I was able to install Selenium Manager for Anaconda. I also noticed that ?print(os.sys.path)` in Spyder returns a different list than echo $PATH in terminal. So I added both /Users/USERNAME/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py and /Users/USERNAME/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/common/service.py to os.sys.path. However, I still get the error that WebDriverException: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home. Commented Nov 25 at 8:17
  • I may have been incorrect there as I've only used Java and there you'd set a system property for the path to the executable. I think in Python you want to specify the executable_path when creating the driver if not using Selenium's new built-in manager. PATH depends on your OS but is only a fallback. (but the error refers to the driver executable, not your .py scripts) This older post might conform with your old version of Selenium: stackoverflow.com/questions/49788257/… Commented Nov 25 at 18:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.