当我尝试运行此代码时,会出现此selenium.common.exceptions.WebDriverException: Message: '' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
问题。我已经从链接下载了文件,并将其解压缩到我的下载文件夹中,如下所示:C:\ Users \ Alexandr \ Downloads \ chromedriver_win32。然后,将可执行二进制文件(C:\ Users \ michael \ Downloads \ chromedriver_win32)的路径放入环境变量“路径”。
我在StackOverflow上找到了相同的thread,他们说我应该在cmd中键入chromedriver
并得到像这样的tarting ChromeDriver 2.15.322448
,就我而言,我得到的是"chromedriver" is not inner or outer command
(是我母语的翻译)。
在这种情况下我该怎么办
from selenium import webdriver
from time import sleep
class InstaBot:
def __init__(self, username, password):
self.driver = webdriver.Chrome()
self.driver.get('https://www.instagram.com/?hl=ru')
sleep(2)
self.driver.find_element_by_xpath("//input[@name=\"username\"]") \
.send_keys(username)
self.driver.find_element_by_xpath("//input[@name=\"password\"]") \
.send_keys(password)
self.driver.find_element_by_xpath('//button[@type="submit"]') \
.click()
sleep(4)
self.driver.find_element_by_xpath('//button[contains(text()."Не сейчас")]').click()
InstaBot('_max_leva_', here is my password)
这是错误代码:
C:\Users\Alexandr\AppData\Local\Programs\Python\Python38-32\python.exe C:/PythonProjects/python/LearningPython/main1.py
Traceback (most recent call last):
File "C:\Users\Alexandr\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\Alexandr\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\Alexandr\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] Не удается найти указанный файл
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/PythonProjects/python/LearningPython/main1.py", line 27, in <module>
InstaBot('_max_leva_', '447781470659')
File "C:/PythonProjects/python/LearningPython/main1.py", line 14, in __init__
self.driver = webdriver.Chrome()
File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\Alexandr\AppData\Local\Programs\Python\Python38-32\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
Process finished with exit code 1
环境变量:
%PATH%:C:\Users\Alexandr\Downloads\chromedriver_win32
答案 0 :(得分:1)
在self.driver = webdriver.Chrome()
中,将路径传递到可执行文件作为括号中的参数。
例如:
self.driver = webdriver.Chrome('C:/user/Downloads/chromedriver.exe')