selenium 安装与 chromedriver安装

安装selenium

selenium可以直接可以用pip安装。

pip install selenium

 

安装chromedriver

 下载

chromedriver的版本一定要与Chrome的版本一致,不然就不起作用。

有两个下载地址:

1、http://chromedriver.storage.googleapis.com/index.html

2、https://npm.taobao.org/mirrors/chromedriver/

当然,你首先需要查看你的Chrome版本,在浏览器中输入chrome://version/

 

 

http://chromedriver.storage.googleapis.com/index.html?path=87.0.4280.20/

 

 

配置

解压压缩包,找到chromedriver.exe复制到chrome的安装目录(其实也可以随便放一个文件夹)。复制chromedriver.exe文件的路径并加入到电脑的环境变量中去。具体的:

 

 

进入环境变量编辑界面,添加到用户变量即可,双击PATH,将你的文件位置(C:\Program Files\Google\Chrome\Application)添加到后面。

 

 

完成后在cmd下输入chromedriver验证是否安装成功:

 

 

测试

未配置环境也可以,例如:

from selenium import webdriver
import time

def main():
    chrome_driver = 'C:\Program Files\Google\Chrome\Application\chromedriver.exe'  #chromedriver的文件位置
    b = webdriver.Chrome(executable_path = chrome_driver)
    b.get('https://www.baidu.com')
    time.sleep(5)
    b.quit()

if __name__ == '__main__':
    main()

 

 

已配置环境变量时

from selenium import webdriver
import time

def main():
    b = webdriver.Chrome()
    b.get('https://www.baidu.com')
    time.sleep(5)
    b.quit()

if __name__ == '__main__':
    main()

 

posted on 2020-12-02 08:47  trp  阅读(119)  评论(0编辑  收藏  举报

导航