含硒Python Chromedriver WinError 2

含硒Python Chromedriver WinError 2,python,selenium,web-scraping,selenium-chromedriver,Python,Selenium,Web Scraping,Selenium Chromedriver,我的代码中出现以下错误: 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

我的代码中出现以下错误:

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
我尝试过更改PATH变量,但更改chromedriver路径无效。有趣的是,这个错误只是偶尔发生,但我还没有找到一个模式。这是我对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)

奇怪的是,它只是偶尔起作用。将此设置为绝对路径,应该可以正常工作

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)


更新:解决方案。事实证明,我定义了驱动程序两次,删除了一个定义,它似乎做得很好。但不确定它为什么会引发那个特定的错误。

奇怪的是,它只在某些时候起作用。将此设置为绝对路径,应该可以正常工作

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)


更新:解决方案。事实证明,我定义了驱动程序两次,删除了一个定义,它似乎做得很好。不确定它为什么会引发这个特定的错误。

如果路径中有chromedriver.exe,我认为您不需要引用chromepath。如果路径中有chromedriver.exe,我认为您不需要引用chromepath。因此,与其输入变量名,不如输入变量值?我会试一试,但这会如何改变事情呢?更新:如果我理解正确,那么它就不起作用了。这确实很奇怪。它有时起作用的事实很难理解。你能试试这样的东西吗?driver=webdriver.Chrome(executable_path='C:/path/to/chromedriver.exe')?我会试一试,但这会如何改变事情呢?更新:如果我理解正确,那么它就不起作用了。这确实很奇怪。它有时起作用的事实很难理解。你能试试这样的东西吗?driver=webdriver.Chrome(executable_path='C:/path/to/chromedriver.exe')我来试试,exe是否可能损坏了?也可能是吗