【问题标题】:Can't use chrome driver for Selenium无法为 Selenium 使用 chrome 驱动程序
【发布时间】:2014-04-03 12:26:04
【问题描述】:

我在使用 Selenium 的 Chrome 驱动程序时遇到问题。我已将 chromedriver 下载并保存到 C:\Chrome:

driver = webdriver.Chrome(executable_path="C:/Chrome/")

使用它会给我以下错误:

Traceback (most recent call last):
  File "C:\Python33\lib\subprocess.py", line 1105, in _execute_child
    startupinfo)
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\selenium\webdriver\chrome\service.py", line 63, in start
    self.service_args, env=env, stdout=PIPE, stderr=PIPE)
  File "C:\Python33\lib\subprocess.py", line 817, in __init__
    restore_signals, start_new_session)
  File "C:\Python33\lib\subprocess.py", line 1111, in _execute_child
    raise WindowsError(*e.args)
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Wilson/Dropbox/xxx.py", line 71, in <module>
    driver = webdriver.Chrome(executable_path="C:/Chrome/")
  File "C:\Python33\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 59, in __init__
    self.service.start()
  File "C:\Python33\lib\site-packages\selenium\webdriver\chrome\service.py", line 68, in start
    and read up at http://code.google.com/p/selenium/wiki/ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.                 Please download from http://chromedriver.storage.googleapis.com/index.html  

任何帮助将不胜感激。

【问题讨论】:

    标签: python google-chrome python-3.x selenium python-3.3


    【解决方案1】:

    您应该指定可执行文件路径,而不是包含可执行文件的目录路径。

    driver = webdriver.Chrome(executable_path=r"C:\Chrome\chromedriver.exe")
    

    【讨论】:

    • 在没有 .exe 扩展的情况下像 Mac 的魅力一样工作
    • 嗨 - driver = webdriver.Chrome(executable_path="C:/Chrome/") 和这个 driver = webdriver.Chrome(executable_path=r"C:\Chrome\chromedriver.exe") 有什么区别注意:我在 Win10 上工作 - 我也有 chromedriver 的问题 - 所以我也在做同样的事情 - 期待收到您的来信
    • @zero,抱歉,我无法访问 Windows 10 机器。请发布一个单独的问题,以便其他使用 windows 机器的人可以回答您。
    【解决方案2】:

    import os   
    from selenium import webdriver
    chromedriver = "C://chromedriver.exe"                                                                     
    os.environ["webdriver.chrome.driver"] = chromedriver                      
    driver =webdriver.Chrome(chromedriver)
    

    【讨论】:

      【解决方案3】:


      适用于 Linux

      1.检查你是否安装了最新版本的chrome浏览器-> "chromium-browser -version"
      2.如果没有,安装最新版本的chrome “sudo apt-get install chromium-browser”
      3.从http://chromedriver.storage.googleapis.com/index.html获取相应版本的chrome驱动
      4.解压chromedriver.zip
      5.将文件移动到/usr/bin目录sudo mv chromedriver /usr/bin
      6. 转到 /usr/bin 目录,你需要运行类似“chmod a+x chromedriver”的东西来标记它可执行。
      7.终于可以执行代码了。

      from selenium import webdriver
      driver = webdriver.Chrome()
      driver.get("http://www.google.com")
      display.stop()
      

      【讨论】:

      • 不是 chromedriver 应该放在 Ubuntu 的 /usr/local/bin 目录下而不是 /usr/bin 下吗?请澄清
      • @vikramvi 只要目录在你的路径上就没有关系
      • @CoreyGoldberg 我更喜欢放在默认目录而不是自定义目录下
      • 晚安,亲爱的 Viky - 非常感谢您的出色回答。顺便说一句 - 剩下一个问题:嗨 - driver = webdriver.Chrome(executable_path="C:/Chrome/") 和这个 driver = webdriver.Chrome(executable_path=r"C:\Chrome\chromedriver.exe") 有什么区别注意:我在 Win10 上工作 - 我也有 chromedriver 的问题 - 所以我也在做同样的事情 - 期待听到来自你
      • 在安装最新版本的 chromium-browser 之前,请确保您使用 >sudo apt install google-chrome-stable 安装了最新版本的 google chrome
      【解决方案4】:

      对于窗户

      从以下位置下载 webdriver:

      http://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip

      将 chromedriver.exe 文件粘贴到“C:\Python27\Scripts”文件夹中。

      现在应该可以了。

      from selenium import webdriver
      driver = webdriver.Chrome()
      

      【讨论】:

        【解决方案5】:

        除了选择的答案(windows风格路径):

        driver = webdriver.Chrome(executable_path=r"C:\Chrome\chromedriver.exe")
        

        注意 "C:\Chrome\chromedriver.exe" 前面的 r,这会使该字符串成为原始字符串。

        如果你不想使用原始字符串,你应该像 \\ 这样转义斜线,这将变成:

        driver = webdriver.Chrome(executable_path="C:\\Chrome\\chromedriver.exe")
        

        或者你可以用/替换\,你会得到这个:

        driver = webdriver.Chrome(executable_path="C:/Chrome/chromedriver.exe")
        

        【讨论】:

          【解决方案6】:

          当您调用 selenium 或任何测试自动化库时,您需要在 Python 中添加此代码,但这也可以在 JavaRuby 中完成。

          options = webdriver.ChromeOptions()
          options.binary_location = '/usr/bin/chromium-browser'
          #All the arguments added for chromium to work on selenium
          options.add_argument("--no-sandbox") #This make Chromium reachable
          options.add_argument("--no-default-browser-check") #Overrides default choices
          options.add_argument("--no-first-run")
          options.add_argument("--disable-default-apps") 
          driver = webdriver.Chrome('/home/travis/virtualenv/python2.7.9/chromedriver',chrome_options=options)
          

          【讨论】:

          • 为什么是--no-sandbox
          【解决方案7】:

          只需将 chromedriver.exe 放在你的 python 文件夹中(在我的例子中:C:\Python27)并使用下面提到的代码,它会为你们工作

          driver = webdriver.Chrome()
          driver.maximize_window()
          driver.get("URL")
          

          【讨论】:

            【解决方案8】:

            对于 Debian/Ubuntu - 它可以工作:

            为 Debian/Ubuntu 安装谷歌浏览器:

            sudo apt-get install libxss1 libappindicator1 libindicator7
            wget https://dl.google.com/linux/direct/google-chrome-
            stable_current_amd64.deb
            
            sudo dpkg -i google-chrome*.deb
            sudo apt-get install -f
            

            安装 ChromeDriver:

            wget -N http://chromedriver.storage.googleapis.com/2.26/chromedriver_linux64.zip
            unzip chromedriver_linux64.zip
            chmod +x chromedriver
            
            sudo mv -f chromedriver /usr/local/share/chromedriver
            sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
            sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
            

            安装硒:

            pip install -U selenium
            

            Python 中的硒:

            from selenium import webdriver
            
            driver = webdriver.Chrome()
            driver.get('https://www.google.co.in/')
            

            【讨论】:

            • 注意:在更新的 ubuntu 版本中可以使用 apt 代替 apt-get
            【解决方案9】:

            您需要做的就是将 Chromedriver.exe 粘贴到 python36-32 文件夹中。您可以像这样使用它:

            from selenium import webdriver
            driver = webdriver.Chrome()
            

            无需反复粘贴路径。


            您可以使用:

            driver = webdriver.Chrome(executable_path="C:/Chrome/chromedriver.exe")
            

            【讨论】:

              【解决方案10】:
              import os
              from selenium import webdriver
              chromedriver = "C:\Drivers\chromedriver_win32\chromedriver.exe"
              os.environ["webdriver.chrome.driver"] = chromedriver
              driver =webdriver.Chrome(chromedriver)
              driver.get("https://www.facebook.com")
              print(driver.title)
              driver.close()
              

              【讨论】:

              • 你能补充一些关于这如何帮助解决最初的问题的信息吗?
              • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的答案添加解释,并说明适用的限制和假设。
              【解决方案11】:

              适用于具有虚拟工作区的 Windows

              首先,检查你的浏览器版本:到chrome浏览器最右边的3个按钮点击它,然后-->帮助-->关于谷歌浏览器

              一旦您确定了我们的浏览器版本,我们必须从this link 下载并安装 chrome 驱动器

              解压 zip 文件夹和过去的 chromedriver.exe 文件 C:\Users\name\virtual_workspace\Scripts

              from selenium import webdriver
              wbdriver = webdriver.Chrome()
              

              【讨论】:

                【解决方案12】:

                此代码不需要驱动文件的路径:

                    from selenium import webdriver
                    from webdriver_manager.chrome import ChromeDriverManager
                
                    driver = webdriver.Chrome(ChromeDriverManager().install())
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多