Python3中selenium的浏览器运行环境

这里只说 Firefox 和 Chrome 浏览器

一、已安装Python3和PyCharm Community环境

二、pip 和 selenium 的安装

1、pip 有的在 Python 中是自带的,cmd 窗口输入:pip list 查看;

  

  

  有可能会提示 pip 版本不匹配,就按提示更新。

2、cmd 窗口 输入:pip install selenium,安装selenium :

这样 pip 和 selenium 安装好了。

三、驱动程序和运行

1、Firefox浏览器:

  版本一定要大于48,在:https://github.com/mozilla/geckodriver/releases/,下载 geckodriver.exe 压缩包,解压后放在 Python 安装目录下,如:D:Python37;

2、Chrome浏览器:

  1)打开浏览器查看版本:chrome://version/

  2)到:http://chromedriver.storage.googleapis.com/index.html? 下载合适的 chromedriver.exe,64位的向下兼容,可以下载32的。解压后放在 Python 安装目录下。 

  3)运行,以chrome浏览器为例:

  没有浏览器驱动程序时会报错:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH。

  百度这一行错误提示可以找到解决方法,就是上面所说的,正确的下载了驱动程序后可以正常运行。

附: 

from selenium import webdriver
import time

chrome_driver = "D:Python37chromedriver.exe"
driver = webdriver.Chrome(executable_path= chrome_driver)
time.sleep(5.5)

driver.maximize_window()
driver.get('http://www.baidu.com/')
driver.find_element_by_id('kw').send_keys('selenium')
#driver.refresh()
driver.quit()

  注意:电脑的浏览器会经常更新版本,过段时间运行脚本,会报:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 75。所以需要重新更新合适的驱动程序 chromedriver.exe。

谢谢查看,笔记持续完善!

2019-10-19更新

原文地址:https://www.cnblogs.com/yuntimer/p/11178530.html