Selenium + Python 之 WebDriver 驱动下载与使用

一、Firefox(火狐)浏览器驱动

下载地址:https://github.com/mozilla/geckodriver/releases/

下载对应驱动:根据自己的操作系统下载相对应的驱动。

使用方法:把文件存放在 python 根目录下,例如:C:\xxx\Python\Python38 下。(安装最新版的火狐,必须高于 48 版本)

代码示例:

from selenium import webdriver

# 启动浏览器

driver = webdriver.Firefox()

二、Chrome(google)浏览器驱动

下载地址:http://chromedriver.storage.googleapis.com/index.html 或 https://sites.google.com/a/chromium.org/chromedriver/home

. 下载地址:http://chromedriver.storage.googleapis.com/index.html 或 https://sites.google.com/a/chromium.org/chromedriver/home

下载地址 129 130 等版本:https://googlechromelabs.github.io/chrome-for-testing/#stable

下载对应驱动:根据自己的操作系统下载相对应的驱动。

使用方法:把文件存放在 python 根目录下,例如:C:\xxx\Python\Python38 下。

代码示例:

from selenium import webdriver

# 启动浏览器

driver = webdriver.Chrome()

三、IE 浏览器驱动

下载地址:http://selenium-release.storage.googleapis.com/index.html

下载对应驱动:根据自己的操作系统下载相对应的驱动。

使用方法:把文件存放在 python 根目录下,例如:C:\xxx\Python\Python38 下。需要注意的是,如果要打开 IE 浏览器的话,需要在浏览器的 Internet 选项中的安全页里有 4 个安全选项,Internet、本地 Internet、受信任的站点、受限制的站点,这 4 个里面都有一个启用保护模式,都需要勾选上才可以。

代码示例:

from selenium import webdriver

# 启动浏览器

driver = webdriver.Ie()

四、Microsoft Edge (EdgeHTML)浏览器驱动

下载地址:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

官方下载地址:https://developer.microsoft.com/zh-cn/microsoft-edge/tools/webdriver/?form=MA13LH#downloads

官方具体目录下载地址:https://msedgewebdriverstorage.z22.web.core.windows.net/?prefix=131.0.2903.146/

下载对应驱动:根据自己的操作系统下载相对应的驱动。

使用方法:把文件存放在 python 根目录下,例如:C:\xxx\Python\Python38 下。

代码示例:

from selenium import webdriver

# 启动浏览器

driver = webdriver.Edge()

五、Microsoft Edge (Chromium)浏览器驱动

下载地址:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

下载对应驱动:根据自己的操作系统下载相对应的驱动。

六、Opera 浏览器驱动

下载地址:https://github.com/operasoftware/operachromiumdriver/releases

下载对应驱动:根据自己的操作系统下载相对应的驱动。

使用方法:把文件存放在 python 根目录下,例如:C:\xxx\Python\Python38 下。

代码示例:

from selenium import webdriver

# 启动浏览器

driver = webdriver.Opera()

七、Safari 浏览器驱动

该浏览器不用下载驱动,可以直接执行代码。

from selenium import webdriver

# 启动浏览器

driver = webdriver.Safari()