简体   繁体   中英

Python Chromedriver WinError 2 with Selenium

I get this following error in my code:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", 
line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\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

I have tried changing the PATH variable, changing the chromedriver path to no avail. Funny thing is that this error only happens sometimes, I have yet to find a pattern though. This is my chromedriver definition:

options = Options()
options.headless = True
options.add_argument('--log-level=3')
chromepath = str(Path(os.getcwd()).parent) + r'\chromedriver.exe'
driver = webdriver.Chrome(chromepath, options=options)

Weird that it only works sometimes. Set this to an absolute path and it should work fine.

chromepath = str(Path(os.getcwd()).parent) + r'\chromedriver.exe'
print (chromepath) #set the output of this below
driver = webdriver.Chrome("abs path here", options=options)

Update: Solution. Turns out I defined driver twice, removed one definition and it seemed to do fine. Unsure why it raised that particular error though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM