【问题标题】:Chrome driver - SeleniumChrome 驱动程序 - Selenium
【发布时间】:2020-10-21 08:11:55
【问题描述】:

我是 programmin 的新学生,我第一次尝试使用 Chromedriver 和 Selenium。我绝对无法访问下载的文件,不知道为什么。 这是我的输入:

from selenium import webdriver
driver = webdriver.Chrome(executable_path = "/Users/silvia/Desktop/executables/chromedriver")

如下所示,我得到的结果如下。说我的桌面上没有这样的文件,但是有!我尝试了反斜杠,双反斜杠和一切。也许我应该从头开始尝试整个过程!知道为什么不工作吗?提前谢谢!

这是错误信息:

------------------
FileNotFoundError                         Traceback (most recent call last)
/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/common/service.py in start(self)
     71             cmd.extend(self.command_line_args())
---> 72             self.process = subprocess.Popen(cmd, env=self.env,
     73                                             close_fds=platform.system() != 'Windows',

/opt/anaconda3/lib/python3.8/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    853 
--> 854             self._execute_child(args, executable, preexec_fn, close_fds,
    855                                 pass_fds, cwd, env,

/opt/anaconda3/lib/python3.8/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1701                         err_msg = os.strerror(errno_num)
-> 1702                     raise child_exception_type(errno_num, err_msg, err_filename)
   1703                 raise child_exception_type(err_msg)

**FileNotFoundError: [Errno 2] No such file or directory: '/Users/silvia/Desktop/executables/chromedriver'**

During handling of the above exception, another exception occurred:

WebDriverException                        Traceback (most recent call last)
<ipython-input-6-a1abac069770> in <module>
      1 from selenium import webdriver
      2 
----> 3 driver = webdriver.Chrome(executable_path = "/Users/silviafabbi/Desktop/executables/chromedriver.exe")

/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
     71             service_args=service_args,
     72             log_path=service_log_path)
---> 73         self.service.start()
     74 
     75         try:

/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/common/service.py in start(self)
     79 except OSError as err:
     80 if err.errno == errno.ENOENT:
---> 81 raise WebDriverException(
     82 "'%s' executable needs to be in PATH. %s" % (
     83 os.path.basename(self.path), self.start_error_message)

WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

【问题讨论】:

  • Chrome 驱动程序一直有这个问题。所以我建议你使用webdriver_manage,这样你就不必链接chrome驱动可执行脚本了。

标签: python selenium-chromedriver


【解决方案1】:

我不知道你是否已经解决了,但这也发生在我身上,我意识到下载 chromedriver 后我忘记将文件放在 python.exe 所在的同一目录中。通常,您可以通过 (C:) --> Users --> your User 找到您的 python.exe 文件,最后是 anaconda3 目录(考虑到您已经安装了我强烈推荐的 anaconda 包)。将您的 chromedriver 文件放在 anaconda3 目录中,该目录与您可以找到 python.exe 的位置完全相同。就是这样!这样做之后,我可以毫无问题地运行我的代码。 来自巴西的问候! ;)

【讨论】:

    【解决方案2】:

    西尔维娅,试试

    C:/Users/silvia/Desktop/executables/chromedriver

    或任何你的硬盘名称

    【讨论】:

    • 非常感谢!我尝试了所有可能的名称(MacintoshHD 代替 C:,Scrivania 代替 Desktop 等等....)。还是不行:(
    【解决方案3】:

    不提供可执行路径:

    你可以使用webdriver_manage(通过pip install webdriver_manager安装)

    from bs4 import BeautifulSoup
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from webdriver_manager.chrome import ChromeDriverManager
    
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
    

    【讨论】:

    • 谢谢!现在它说 ValueError: Could not get version for Chrome with this command: /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version 仍在尝试建议的其他方法,但无论如何都没有成功 :) 我会继续尝试
    • 我对macos一无所知。我认为问题在于 chrome 版本。
    • 其实就是这样。非常感谢您提出正确的方法!
    • 很高兴它有帮助!如果答案有帮助,请接受答案(点击勾选)来关闭问题:)
    猜你喜欢
    • 2017-03-10
    • 2022-01-28
    • 1970-01-01
    • 2022-11-21
    • 2018-10-20
    • 2019-09-22
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多