如何使用默认配置文件启动Chromedriver?

时间:2014-10-06 13:04:40

标签: python google-chrome selenium selenium-webdriver chrome-web-driver

我尝试使用Chromedriver和非默认配置文件加载Chrome 我正在使用这个python代码:

opt = webdriver.ChromeOptions()
chromedriver = r"C:/chromedriver/chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
opt.add_argument(r"user-data-dir=C:\Users\user\AppData\Local\Google\Chrome\User Data\Default_Selenium")
driver = webdriver.Chrome(opt)


但我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\user\workspace\dd\src\start.py", line 29, in <module>
    main()
  File "C:\Users\user\workspace\dd\src\start.py", line 20, in main
    driver = webdriver.Chrome(opt)
  File "C:\Python27\Lib\site-packages\selenium-2.43.0-py2.7.egg\selenium\webdriver\chrome\webdriver.py", line 59, in __init__
    self.service.start()
  File "C:\Python27\Lib\site-packages\selenium-2.43.0-py2.7.egg\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                and read up at http://code.google.com/p/selenium/wiki/ChromeDriver' 

我做错了什么?

1 个答案:

答案 0 :(得分:1)

chromedriver可执行文件应该在PATH环境变量中可用,或者通过executable_path参数Chrome()显式设置:

driver = webdriver.Chrome(executable_path=r'C:/chromedriver/chromedriver.exe',
                          chrome_options=opt)