Python 让 Chrome 通过 Selenium 启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17436598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:12:53  来源:igfitidea点击:

Getting Chrome to launch via Selenium

pythonseleniumselenium-webdriverselenium-chromedriver

提问by haran kumar

Hi all I'm very new to this and am having issues getting an instance of a Chrome browser from selenium in python. I'm using Windows 8. I have downloaded the chromedriver binary and added it to my path but I get the following error in Python:

大家好,我对此很陌生,并且在从 python 中的 selenium 获取 Chrome 浏览器的实例时遇到问题。我使用的是 Windows 8。我已经下载了 chromedriver 二进制文件并将其添加到我的路径中,但在 Python 中出现以下错误:

selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.   

This error occurs for the following line:

以下行会发生此错误:

driver = webdriver.Chrome(executable_path='path\to\chromedriver_win32_2.0')  

Any help is greatly appreciated. Thank you.

任何帮助是极大的赞赏。谢谢你。

回答by Ittiel

Assuming that your path is correct, make sure that you include the chromedriver itself: chromedriver.exe

假设您的路径正确,请确保包含 chromedriver 本身: chromedriver.exe

回答by Yi Zeng

Two ways to set it, you somehow mixed up.

设置它的两种方法,你不知何故混淆了。

  • Put the chromedriver.exe's path into PATH(on Windows), so your PATHsetting is correct, but you need to call the default constructor.

    driver = webdriver.Chrome()

  • Specify the path in webdriver.Chrome(executable_path='some path'). Here you need the full path to the executable, not the directory.

    webdriver.Chrome(executable_path=r'C:\Users\HaranKumar\Downloads\chromedriver_win32_2.0\chromedriver.exe')

  • chromedriver.exe的路径放入PATH(在 Windows 上),因此您的PATH设置是正确的,但您需要调用默认构造函数。

    driver = webdriver.Chrome()

  • 中指定路径webdriver.Chrome(executable_path='some path')。在这里,您需要可执行文件的完整路径,而不是目录。

    webdriver.Chrome(executable_path=r'C:\Users\HaranKumar\Downloads\chromedriver_win32_2.0\chromedriver.exe')

Choose either one you want.

选择您想要的任何一种。

回答by super1ha1

Update 2016

2016 年更新

The following solution works for me, with WebDriver 3.0.1, Chrome Driver 2.25.426923, Window 7

以下解决方案适用于我,WebDriver 3.0.1、Chrome 驱动程序 2.25.426923、Window 7

    System.setProperty("webdriver.chrome.driver","D:\workspace\chromedriver.exe");
    WebDriver driver;
    driver = new ChromeDriver();

*Note:

*笔记:

回答by kouichi

I used the following and it worked! Thanks!

我使用了以下方法并且有效!谢谢!

driver = webdriver.Chrome(executable_path=r'C:\chromedriver.exe')
#put your own path between the ''

回答by Srinidhi Vn

Even if you have chromedriver.exe in the PATH, its necessary to have chromedriver.exe in the folder where your executable scripts are present(atleast so is the case when it comes to python scripts)

即使你在 PATH 中有 chromedriver.exe,也有必要在你的可执行脚本所在的文件夹中有 chromedriver.exe(至少在涉及 python 脚本时是这样)