简体   繁体   English

Python Chromedriver WinError 2 与 Selenium

[英]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.我尝试更改 PATH 变量,更改 chromedriver 路径无济于事。 Funny thing is that this error only happens sometimes, I have yet to find a pattern though.有趣的是,这个错误只是偶尔发生,但我还没有找到一个模式。 This is my chromedriver definition:这是我的 chromedriver 定义:

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.不确定为什么它会引发该特定错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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