selenium+python+chrome环境搭建遇到的问题

今天试着搭建了selenium+python+chrome环境,为之后学习web自动化做好准备。搭建的过程中遇到了一些问题,记录下来给新的同学排雷哦。

  • 问题一:‘chromedriver’ executable needs to be in PATH问题
  • 解决方案:
    1、首先需要下载Chromedriver,下载后得到的是一个chromedriver.exe文件。注意:下载Chromedriver的时候选择版本一定要和chrom浏览器的版本匹配上
    chromedriver下载地址:http://npm.taobao.org/mirrors/chromedriver/
    2、解压后将chromedriver.exe拷贝至谷歌浏览器目录(浏览器输入chrome://version/ 可执行文件路径就是谷歌浏览器的目录啦
    3、同时将解压后的chromedriver.exe添加到python根目录。
    4、将谷歌浏览器路径添加到环境变量path中。(C:\Users\zhangjing\AppData\Local\Google\Chrome\Application)。
  • 问题二:ImportError: cannot import name webdriver报错
  • 解决方案:
    1、因为我习惯性的新建py文件时,命名为selenium.py才导致了此问题。Python会先导入这个文件,然后再导入标准库里面的selenium.py。
    2、我们只需要重命名py文件名或者删除文件重新创建一个文件名不为selenium的py文件即可。
  • python代码验证下是否搭建成功
   from selenium import webdriver driver = webdriver.Chrome()#chrome浏览器
   driver.get("http://baidu.com/")#访问百度
   print(driver.title)#打印标题
   driver.quit()

你可能感兴趣的:(python,selenium,selenium,python,chrome)