【问题标题】:FileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec' error using Selenium on MAC OSXFileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec' 在 MAC OSX 上使用 Selenium 时出错
【发布时间】:2021-03-03 07:22:33
【问题描述】:

我正在尝试学习如何使用 selenium 和 python,并且我正在尝试关注此视频: https://www.youtube.com/watch?v=Xjv1sY630Uc&ab_channel=TechWithTim

这是我的代码:

from selenium import webdriver

PATH = "/Users/fuadhafiz/Documents\chromedriver.exec"
driver = webdriver.Chrome(PATH)

driver.get("https://stackoverflow.com")

但这就是终端上不断出现的内容(我正在使用 VS Code 并且在 mac 上)

/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 "/Users/fuadhafiz/Documents/Python Projects/Selenium Automation /Web Scraping (1)/web_scraping_attempt.py"
fuadhafiz@Fuads-iMac Web Scraping (1) % /Library/Frameworks/Python.framework/Versions/3.9/bin/python3 "/Users/fuadhafiz/Documents/Python Projects/Selenium Automation /Web Scraping (1)/web_scraping_attempt.py"
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 947, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1819, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/fuadhafiz/Documents/Python Projects/Selenium Automation /Web Scraping (1)/web_scraping_attempt.py", line 4, in <module>
    driver = webdriver.Chrome(PATH)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 81, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'Documents\chromedriver.exec' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

在“问题”部分

字符串中的反斜杠异常:'\c'。字符串常量可能缺少 r 前缀。

这是 chrome 驱动程序被保存的情况:

【问题讨论】:

    标签: python macos selenium google-chrome selenium-chromedriver


    【解决方案1】:

    你快到了。在 系统中,不需要ChromeDriver 二进制文件的扩展名。因此,您的代码块将是:

    from selenium import webdriver
    
    PATH = "/Users/fuadhafiz/Documents/chromedriver"
    driver = webdriver.Chrome(PATH)
    driver.get("https://stackoverflow.com")
    

    参考文献

    您可以在以下位置找到一些详细的相关讨论:

    【讨论】:

    • @7UAD 查看更新的代码块和参考讨论。
    • 您好,我已经尝试过您的代码,它提出了这个:raise WebDriverException(selenium.common.exceptions.WebDriverException: Message: 'Documents\chromedriver' 可执行文件需要在 PATH 中。请参阅sites.google.com/a/chromium.org/chromedriver/home
    • @7UAD 所以 ...FileNotFoundError: [Errno 2] No such file or directory... 在这个问题中的错误现在已经解决了。对于下一个错误,请随时根据您的新要求提出新问题。
    • 好的,谢谢。对不起,我对这个堆栈溢出的东西很陌生
    【解决方案2】:

    查看错误消息,您使用了错误的文件扩展名 .exec 而不是 .exe

    FileNotFoundError: [Errno 2] No such file or directory: '/Users/fuadhafiz/Documents\\chromedriver.exec'
    

    试试这个

    from selenium import webdriver
    
    PATH = "/Users/fuadhafiz/Documents\chromedriver.exe"
    driver = webdriver.Chrome(PATH)
    
    driver.get("https://stackoverflow.com")
    

    我也在用mac,所以在默认PATH中安装chromedriver可以省去每次添加路径的麻烦

    brew cask install chromedriver
    

    现在您可以简单地调用

    driver = webdriver.Chrome()
    

    【讨论】:

      猜你喜欢
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 2022-01-24
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多