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/
例如我的版本是72.0.3626,所以下载
配置
解压压缩包,找到chromedriver.exe复制到chrome的安装目录(其实也可以随便放一个文件夹)。复制chromedriver.exe文件的路径并加入到电脑的环境变量中去。具体的:
进入环境变量编辑界面,添加到用户变量即可,双击PATH,将你的文件位置(C:\Program Files (x86)\Google\Chrome\Application\)添加到后面。
完成后在cmd下输入chromedriver验证是否安装成功:
测试
未配置环境也可以,例如:
from selenium import webdriver
import time
def main():
chrome_driver = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe' #chromedriver的文件位置
b = webdriver.Chrome(executable_path = chrome_driver)
b.get('https://www.google.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()
如果运行时提示
很可能是chromedriver的版本不对(不要问我怎么知道的)。
参考链接:
1、https://blog.csdn.net/qq_41429288/article/details/80472064
2、https://www.cnblogs.com/LeslieForever/p/8317158.html
个性签名:时间会解决一切
还没有评论,来说两句吧...