Reputation: 13
I am running a selenium script and I would like to run my code without opening the browser using chrome options. I tried setting driver to PATH and then changing it to options=op but it showed the same error (The file path in my code is accurate). I followed the second answer on this post and tried running chromedriver.exe in cmd (Not sure if this is relevant in but i saw this in another post and thought it could be of use). The snippet below was the output:
c:\Program Files (x86)>chromedriver
Starting ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Here is my code:
from selenium import webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
op = webdriver.ChromeOptions()
op.add_argument('headless')
# driver = webdriver.Chrome(PATH)
driver = webdriver.Chrome(options=op)
And here is the error I am receiving:
Traceback (most recent call last):
File "C:\Users\soodr\PythonFiles\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\soodr\PythonFiles\Lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\soodr\PythonFiles\Lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/soodr/PycharmProjects - Copy/pythonProject1/linkedenInfo.py", line 16, in <module>
driver = webdriver.Chrome(options=op)
File "C:\Users\soodr\PythonFiles\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\soodr\PythonFiles\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Is there something wrong about the way I am defining driver?
Upvotes: 0
Views: 249
Reputation: 31
your string is wrong. if you wanna use \
in a string, do a raw string that is PATH = r"C:\Program Files (x86)\chromedriver.exe"
Upvotes: 1