Reputation: 25
I am trying to learn how to use selenium and python as well i am trying to follow this video : https://www.youtube.com/watch?v=Xjv1sY630Uc&ab_channel=TechWithTim
This is the code I have :
from selenium import webdriver
PATH = "/Users/fuadhafiz/Documents\chromedriver.exec"
driver = webdriver.Chrome(PATH)
driver.get("https://stackoverflow.com")
But this is what keeps coming up on the terminal ( I am using VS Code and am on mac)
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 "/Users/fuadhafiz/Documents/Python Projects/Selenium Automation /Web Scraping (1)/web_scraping_attempt.py"
fuadhafiz@Fuads-iMac Web Scraping (1) % /Library/Frameworks/Python.framework/Versions/3.9/bin/python3 "/Users/fuadhafiz/Documents/Python Projects/Selenium Automation /Web Scraping (1)/web_scraping_attempt.py"
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 947, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1819, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/fuadhafiz/Documents/Python Projects/Selenium Automation /Web Scraping (1)/web_scraping_attempt.py", line 4, in <module>
driver = webdriver.Chrome(PATH)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'Documents\chromedriver.exec' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
and in the "problems" section
Anomalous backslash in string: '\c'. String constant might be missing an r prefix.
This is were the chrome driver is saved :
Upvotes: 1
Views: 7783
Reputation: 193308
You were almost there. In macos systems the extension for the ChromeDriver binary isn't required. So effectively your code block will be:
from selenium import webdriver
PATH = "/Users/fuadhafiz/Documents/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://stackoverflow.com")
You can find a couple of detailed relevant discussions in:
Upvotes: 2
Reputation: 288
see error message you are using wrong file extension .exec instead of .exe
FileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec'
try this instead
from selenium import webdriver
PATH = "/Users/fuadhafiz/Documents\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://stackoverflow.com")
I am also using mac so install chromedriver in default PATH saves so much trouble of adding path each time
brew cask install chromedriver
now you can simply call
driver = webdriver.Chrome()
Upvotes: 0