【问题标题】:'chromedriver_mac' executable needs to be in PATH'chromedriver_mac' 可执行文件需要在 PATH 中
【发布时间】:2020-12-30 15:43:04
【问题描述】:

我正在尝试使用 Selenium 模块创建一个网络爬虫。用户拥有 Mac 系统。我的项目文件夹包括

./scraper    The main file
   ./lib     The folder where chromedriver_mac is placed
   ./src     py file is here

所以,我使用 os.chdir() 返回一个文件夹,然后找到“lib”文件夹。

os.chdir("..")
os.chdir("lib")
browser = webdriver.Chrome("chromedriver_mac")

它在 Windows 上完美运行。但是,当涉及到 MAC 时,它会抛出异常。

FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver_mac'
.
.

During handling of the above exception, another exception occurred:
.
.
WebDriverException(selenium.common.exceptions.WebDriverException: Message: 'chromedriver_mac' executable needs to be in PATH.

我需要找到一个使用相对路径的解决方案。绝对路径对我来说似乎不是解决方案,因为会有不同的用户。

谢谢。

【问题讨论】:

  • 你试过打电话给browser = webdriver.Chrome("../lib/chromedriver_mac")吗?并不是说它在逻辑上与您正在做的事情有任何不同,但操作系统差异可能会令人痛苦,这应该普遍适用。
  • @ABear 它没有用。当我将绝对位置设置为“/Users/xxx/Downloads/chromedriver”时,它的工作原理很奇怪。我真的迷路了。
  • 是在下载中还是在../lib 中?

标签: python macos selenium webdriver


【解决方案1】:

尝试使用:

from webdriver_manager.chrome import ChromeDriverManager

要安装它:

pip3 install webdriver-manager

然后你可以调用:

driver = webdriver.Chrome(ChromeDriverManager().install())

这消除了始终下载 chrome 驱动程序以匹配您的 chrome 版本的需要,并且不必提供路径(这是这里的问题)。 每次启动代码时,它都会进行验证并安装(如果需要)正确的。

关于这个包的更多信息here

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 2020-07-22
    • 2017-12-08
    • 2017-11-21
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2018-02-14
    相关资源
    最近更新 更多