from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = "D:\\software\\chrome\chrome.exe"
chrome_driver_binary = "D:\software\Anaconda\Scripts\chromedriver.exe"
browser = webdriver.Chrome(chrome_driver_binary, chrome_options=options)
browser.get('http://www.baidu.com/')
1.首先pip install selenium
2.查看自己chrome的版本,搜索chromedriver download到官网下载相应的驱动,放在python/anaconda的scripts目录下
3.点击我的电脑->属性->高级系统设置->环境变量->找到path编辑,添加chromedrivers的存放路径,具体到chromedrivers.exe
4.以上代码即可驱动chrome打开百度。
过程中会出现以下报错:
Message: 'chromedriver' executable needs to be in PATH:需要将chromedriver.exe添加到环境变量中,第3步
Message: unknown error: cannot find Chrome binary:chromedriver找不到chrome的路径,所以需要在代码中第三行指定好chrome的路径
Message: unknown error: chrome failed to start:这个事因为第三行中路径没有具体指定到chrome.exe出现打不开chrome的报错