【问题标题】:Selenium Path Doesn't make sense硒路径没有意义
【发布时间】:2018-07-25 19:41:23
【问题描述】:

运行一个快速脚本

from selenium import webdriver

path = r"C:/Users/andre/Desktop/chromedriver.exe"
driver = webdriver.Chrome(path)

driver.get('https://www.google.ca')

输出是

andrefu@LAPTOP-1011FFMG:/mnt/c/Users/andre/Desktop/Pardee Lab$ python3 Scrape_RNAfold.py
Traceback (most recent call last):
  File "/home/andrefu/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/home/andrefu/anaconda3/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/home/andrefu/anaconda3/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/andre/Desktop/chromedriver.exe': 'C:/Users/andre/Desktop/chromedriver.exe'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Scrape_RNAfold.py", line 7, in <module>
    driver = webdriver.Chrome(path)
  File "/home/andrefu/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
    self.service.start()
  File "/home/andrefu/anaconda3/lib/python3.6/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.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

我的 chromedriver.exe 在我的桌面 所以,我真的不知道为什么它说 chromedriver 不起作用。

我尝试了不同的路径,例如

path = 'C:\Users\andre\Desktop\chromedriver.exe'
path = 'C:\Users\andre\Desktop'
path = 'C:/Users/andre/Desktop/chromedriver.exe'
path = 'C:/Users/andre/Desktop'

即使将我的 chromedriver 与我的代码 Scrape_RNAfold.py 放在同一个文件夹中,也可以使用完整路径名和 driver = webdriver.Chrome() 运行它。

我还尝试了在字符串之前使用原始 'r' unicode 并且不使用它并使用单引号和双引号。有人可以帮我确定我哪里出错了吗?

【问题讨论】:

  • 尝试将其与 Python3 可执行文件放在同一文件夹中并用作driver = webdriver.Chrome()
  • @Andersson 感谢您抽出宝贵时间,在原始帖子中您可以看到我已经尝试过了。这里是my output
  • 没有。我的意思不是和你的脚本在同一个文件夹中,而是在同一个文件夹中 python3.exe
  • @Andersson 对此感到抱歉,我正在使用 WSL,所以我的 python3.exe 一直在我的 /home 中。这里是my output
  • @Andersson 我不知道把它放到我的 python3.exe 中做了什么,但它一直给我 Invalid 参数。注意:它现在不允许我做任何事情,我尝试了pip freeze | grep splinter,它给了我一个无效的论点! (哦,不,我不知道我做了什么......)我现在从那个文件夹中删除了 chromedriver.exe。

标签: python-3.x selenium selenium-webdriver selenium-chromedriver windows-subsystem-for-linux


【解决方案1】:

路径分隔符(即/)和引号(即"")之间似乎有些不匹配

您需要从ChromeDriver - WebDriver for Chrome 下载最新的 ChromeDriver 并将其存储在系统中的任何位置。在 Windows 操作系统 上,解压缩二进制文件并执行以下操作:

  • 在您的程序中,将 Key executable_pathValue 一起传递,以引用Chrome 驱动程序
  • 在提及 ChromeDriver绝对路径时,请在双引号内使用 双反斜杠,即\\,即" "
  • 或在单引号内使用单反斜杠,即\,即" " 以及原始的r 开关,如下所示。
  • 所以你的代码块将是:

    from selenium import webdriver
    
    driver = webdriver.Chrome(executable_path=r'C:\Users\andre\Desktop\chromedriver.exe')
    driver.get('https://www.google.ca')
    

【讨论】:

  • 感谢您的回复。我之前试过,抱歉我没有提到它。这是my output 你可以看到, exectuable_path 不被识别为参数。删除 executable_path 时,我收到与以前类似的错误。
  • 编辑:注意到我拼写错误的可执行文件,更改它,我得到same error as before
  • 您能否使用错误跟踪日志更新邮件问题以便进一步分析?
【解决方案2】:

我发现使用driver = './chromedriver.exe' 是最有效的方式,而不是必须给出PATH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2010-10-11
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多