【问题标题】:chromedriver.exe is in PATH, but selenium gives me an errorchromedriver.exe 在 PATH 中,但 selenium 给了我一个错误
【发布时间】:2018-08-21 01:41:58
【问题描述】:

我在 Windows 10 中使用 bash shell,这让一切都感觉像一个 unix 系统,我正在尝试运行以下 python2.7 代码:

    from selenium import webdriver

    driver = webdriver.Chrome()

但是,我收到以下错误代码:

Traceback (most recent call last):
File "seleniumtest.py", line 3, in <module>
driver = webdriver.Chrome()
File "/home/eirik/.local/lib/python2.7/sitepackages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
self.service.start()
File "/home/eirik/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see 
https://sites.google.com/a/chromium.org/chromedriver/home

通过谷歌搜索,我发现这是一个很常见的问题,我必须下载 chromedriver 并指定它的位置。所以我把上面的改成这样:

    driver = webdriver.Chrome(executable_path = r'/path/to/chromedriver.exe')

但我得到了相同的结果。之后,我进入 Windows 环境变量并确保我放置 chromedriver 的文件夹被读取为 PATH。是的。

为了检查这个 PATH 是否也被 bash shell 读取,我写了以下内容:

    echo $PATH | tr ":" "\n" | nl

在我的 bash shell 中。这给出了输出:

 1  /home/eirik/bin
 2  /home/eirik/.local/bin
 3  /usr/local/sbin
 4  /usr/local/bin
 5  /usr/sbin
 6  /usr/bin
 7  /sbin
 8  /bin
 9  /usr/games
10  /usr/local/games
11  /mnt/c/webdriver
12  /mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common
13  /mnt/c/ProgramData/Oracle/Java/javapath
14  /mnt/c/Program Files (x86)/Intel/iCLS Client
15  /mnt/c/Program Files/Intel/iCLS Client
16  /mnt/c/Windows/System32
17  /mnt/c/Windows
18  /mnt/c/Windows/System32/wbem
19  /mnt/c/Windows/System32/WindowsPowerShell/v1.0
20  /mnt/c/Program Files (x86)/GtkSharp/2.12/bin
21  /mnt/c/Program Files (x86)/Skype/Phone
22  /mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL
23  /mnt/c/Program Files/Intel/Intel(R) Management Engine Components/DAL
24  /mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/IPT
25  /mnt/c/Program Files/Intel/Intel(R) Management Engine Components/IPT
26  /mnt/c/Windows/System32
27  /mnt/c/Windows
28  /mnt/c/Windows/System32/wbem
29  /mnt/c/Windows/System32/WindowsPowerShell/v1.0
30  /mnt/c/Program Files (x86)/Windows Live/Shared
31  /mnt/c/Program Files/MiKTeX 2.9/miktex/bin/x64
32  /mnt/c/Users/Hauge/AppData/Local/Microsoft/WindowsApps
33  /snap/bin
34  /mnt/c/webdriver/

其中 /mnt/c/webdriver/ 是 chromedriver.exe 文件所在的位置。我不知道下一步该做什么。我已经尝试了我能找到的一切,但我似乎无法让 selenium 接受我的 chromedriver。有人知道该怎么做吗?

【问题讨论】:

  • 这条语句driver = webdriver.Chrome(executable_path = r'/path/to/chromedriver.exe') 应该可以在不添加环境变量的情况下工作,您是否使用了完整路径? IE。如果它位于 C 下:您使用 C:\My\Chrome\Path\Chromedriver.exe
  • 是的。我尝试将驱动程序放在同一个文件夹中,只写文件名以及写出整个路径。
  • 当您传递完整路径时,它会为您提供完全相同的Traceback?
  • 请同时参考this,以防您无法以正确的格式传递路径。
  • 感谢您的所有回答!我已经尝试了你的最后一个提示,我得到了与上面相同的错误,但是这部分发生了变化:消息:'C:\ webdriver \ chromedriver.exe'可执行文件需要在PATH中

标签: python bash selenium windows-10 python-2.x


【解决方案1】:

我相信您的问题是您试图让您的 Ubuntu 子系统在需要 Linux 驱动程序时访问 Windows 驱动程序。下载适用于 linux64 的 chromedriver 并确保它在您的路径中。

【讨论】:

  • 这很可能是解决问题的方法。但是,我无法测试它,因为我的解决方案使脚本工作。但我认为这会是一个更好的方法。
【解决方案2】:

我找到了解决办法! 通过将 chromedriver.exe 文件放在与脚本相同的文件夹中并使用 ./chromedriver.exe 运行它,脚本就可以从该文件夹访问它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    相关资源
    最近更新 更多