我是靠谱客的博主 贪玩太阳,这篇文章主要介绍selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT,现在分享给大家,希望可以做个参考。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def share_browser():
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')

    # path是你自己的chrome浏览器的文件路径
    # path = r'C:Program FilesGoogleChromeApplicationchrome.exe'
    path = r'C:Program FilesGoogleChromeApplicationchrome.exe'
    chrome_options.binary_location = path

    browser = webdriver.Chrome(chrome_options=chrome_options)

    return browser

browser = share_browser()

url = 'https://www.baidu.com'

browser.get(url)

browser.save_screenshot('baidu.png')

在测试handless方法时,按照视频中的代码出现如下错误:

selenium.common.exceptions.WebDriverException: 
Message: 'chromedriver' executable needs to be in PATH. 
Please see https://chromedriver.chromium.org/home

 因为未将chromedriver设置在环境变量中,因此需要对代码进行如下修改:

browser = webdriver.Chrome(chrome_options=chrome_options, executable_path='../chromedriver.exe')

完整代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def share_browser():
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')

    # path是你自己的chrome浏览器的文件路径
    # path = r'C:Program FilesGoogleChromeApplicationchrome.exe'
    path = r'C:Program FilesGoogleChromeApplicationchrome.exe'
    chrome_options.binary_location = path

    browser = webdriver.Chrome(chrome_options=chrome_options, executable_path='../chromedriver.exe')

    return browser

browser = share_browser()

url = 'https://www.baidu.com'

browser.get(url)

browser.save_screenshot('baidu.png')

最后

以上就是贪玩太阳最近收集整理的关于selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT的全部内容,更多相关selenium.common.exceptions.WebDriverException:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(112)

评论列表共有 0 条评论

立即
投稿
返回
顶部