chromedriver 可执行文件需要位于 PATH 中

问题描述 投票:0回答:3

我知道这个问题已经被问过很多次了,我也尝试了所有的答案,但它对我不起作用。我已经在一个简单的脚本上尝试了选项和 PATH 方法来提取网页的标题,并且它有效。但对于所有这些代码,它给了我上述错误

import requests
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

HEADERS = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'}
options = webdriver.ChromeOptions() 
options.add_experimental_option("excludeSwitches", ["enable-logging"])
PATH = r"C:\Users\hp\Downloads\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(options=options , executable_path=PATH)

def get_current_url(url, jobTitle, location):
    driver = webdriver.Chrome()
    driver.get(url)
    time.sleep(3)
    driver.find_element_by_xpath('//*[@Id="text-input-what"]').send_keys(jobTitle)
    time.sleep(3)
    driver.find_element_by_xpath('//*[@Id="text-input-where"]').send_keys(location)
    time.sleep(3)
    driver.find_element_by_xpath('/html/body/div').click()
    time.sleep(3)
    try:
        driver.find_element_by_xpath('//*[@id="jobsearch"]/button').click()
    except:
        driver.find_element_by_xpath('//*[@id="whatWhereFormId"]/div[3]/button').click()
        current_url = driver.current_url
    return current_url

current_url = get_current_url('https://pk.indeed.com/', 'Python Developer', 'Karachi')
print(current_url)
Traceback (most recent call last):
  File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 969, in __init__      
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1438, 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 "e:\de-projects\Indeed-scraper.py", line 31, in <module>
    current_url = get_current_url('https://pk.indeed.com/', 'Python Developer', 'Karachi')
  File "e:\de-projects\Indeed-scraper.py", line 15, in get_current_url
    driver = webdriver.Chrome()
  File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
    super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 89, in __init__
    self.service.start()
  File "C:\Users\hp\AppData\Local\Programs\Python\Python310\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://chromedriver.chromium.org/home
python selenium selenium-webdriver selenium-chromedriver
3个回答
3
投票

解决方案

您可以使用

ChromeDriverManager

摆脱所有驱动程序、版本问题
  • 如果您正在使用 ChromeDriverManager - Webdriver Manager Python 你不必显式下载

    ChromeDriver
    因为它 自动下载。

  • 如果您想使用下载的

    ChromeDriver
    ,您可以避免使用
    ChromeDriverManager - Webdriver Manager

使用

ChromeDriverManager
,您可以使用以下代码块:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.maximize_window()
driver.get("https://www.google.com")

下载特定版本的 ChromeDriver 您可以使用以下代码块:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

s = Service('C:/Users/hp/Downloads/chromedriver/chromedriver.exe')
driver = webdriver.Chrome(service=s)

注意:如果您使用的是

Linux / MAC O SX
系统,您需要剥离扩展部分,即
.exe
,因为它仅适用于
windows
平台。


1
投票

而不是

PATH = r"C:\Users\hp\Downloads\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(options=options , executable_path=PATH)

尝试

from selenium.webdriver.chrome.service import Service

webdriver_service = Service(r'C:\Users\hp\Downloads\chromedriver\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service)

0
投票

您有 2 个选择。

首先,您可以将

C:\Users\hp\Downloads\chromedriver\chromedriver.exe
添加到系统路径。请按照以下指南操作。

https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/

或者第二,当你使用r字符串时,你需要像这样改变斜杠。如果你翻转它应该会起作用。

PATH = r"C:/Users/hp/Downloads/chromedriver/chromedriver.exe"
© www.soinside.com 2019 - 2024. All rights reserved.