在 Python 自动化测试与网页数据采集领域,Selenium 是最核心的工具之一。很多人习惯用 Chrome 浏览器,但实际上,基于 Chromium 内核的 Microsoft Edge 浏览器,凭借其系统原生支持、更低的资源占用和同样优秀的兼容性,正在成为越来越多人的选择。本文将带你从环境搭建开始,一步步掌握如何用 Selenium 驱动 Edge 浏览器,并通过实战案例完成完整的自动化流程。
一、环境准备:Edge 浏览器与驱动安装
- 确认 Edge 浏览器版本
首先要确保你的 Edge 浏览器是最新版本,并且记下它的版本号。
• 打开 Edge → 点击右上角「···」→ 「设置」→ 「关于 Microsoft Edge」
• 记下版本号(例如:144.0.3719.104)

- 下载匹配的 EdgeDriver
驱动是 Selenium 和浏览器之间的桥梁,版本必须严格匹配。
• 官方下载地址:Microsoft EdgeDriver

• 官网界面往下,根据你的系统(Windows/macOS/Linux)和 Edge 版本下载对应的驱动
• 解压后得到 msedgedriver.exe(Windows)或 msedgedriver(macOS/Linux)
- 配置驱动环境
为了让 Python 能全局找到驱动,推荐把驱动文件放到 Python 的 Scripts 目录下(例如:C:\Python311\Scripts)。

或者,也可以在代码里直接指定驱动路径:
driver = webdriver.Edge(executable_path=r"C:\Tools\msedgedriver.exe")
- 安装selenium库
pip install selenium==4. 11. 0 -i https://pypi. mirrors. ustc. edu. cn/simple/
(以selenium4.11.0为例)

二、驱动浏览器
在selenium库源代码文件下的webdriver中课查看所有支持的浏览器类型,webdriver使用形式如下
(以Edge为例):
webdriver.edge()
驱动 Edge 浏览器
from selenium import webdriver
from selenium.webdriver.edge.options import Options
edge_options = Options()
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"driver = webdriver.Edge(options=edge_options)
三、加载网页
- get()方法:get(url),在当前浏览器会话中加载url指向的网页
python
import time
from selenium import webdriver
from selenium.webdriver.edge.options import Options
edge_options = Options()
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver = webdriver.Edge(options=edge_options)
driver.get('https://www.baidu.com/')
input('dengdai')
time.sleep(1)

- execute_script()方法:execute_script(script,*args),用于打开多个标签页,即在同一浏览器中打开多个网页。
python
import time
from selenium import webdriver
from selenium.webdriver.edge.options import Options
edge_options=Options()
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver = webdriver.Edge(options=edge_options)
driver.execute_script("window.open('https://www.bilibili.com/','_self');")
driver.execute_script("window.open('https://www.shuyishe.com/','_blank');")
driver.execute_script("window.open('https://www.shuyishe.com/course','_blank');")
input('dengdai')
time.sleep(1)

四、获取渲染后的代码
通get()方法获取浏览器中的网页资源后,浏览器将自动渲染网页源代码内容,并生成渲染后的内容,这时使用page_source()方法即可获取渲染后的网页代码。
python
from selenium import webdriver
from selenium.webdriver.edge.options import Options
edge_options = Options()
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver = webdriver.Edge(options=edge_options)
driver.get('http://www.baidu.com')
print(driver.page_source)
input('dengdai')

五、实现批量下载网页资源
python
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
edge_options = Options()
edge_options.binary_location =r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver=webdriver.Edge(options=edge_options)
driver.get('https://image.baidu.com/search/index?tn=baiduimage&ie=utf-8&word=热巴')#执行JavaScript滚动操作
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5)
img_list=driver.find_elements(By.XPATH, value="//img[@class='img_7rRSL']")
i=1
for img in img_list:
img_url = img.get_attribute("src")
img_data = requests.get(img_url)
with open(f"./迪丽热巴1/{i}.png", 'wb') as f:
f.write(img_data.content)
i += 1

六、获取和操作网页元素
1.获取网页中的指定元素
在获取网页某个元素后,可以使用以下方法对此元素进行相应操作。
• tag_name()方法:获取元素的名称。
• text()方法:获取元素的文本内容。
• click()方法:单击此元素。
• submit()方法:提交表单。
• send_keys()方法:模拟输入信息。
• size()方法:获取元素的尺寸
- 在元素中输入信息
send_keys()方法可以实现在元素中输入信息。
python
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
edge_options = Options()
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver = webdriver.Edge(options=edge_options)
driver.get('https://www.ryjiaoyu.com/')
driver.find_elements(by=By.TAG_NAME,value="input")[0].send_keys("Python")
input('dengdai')

实现在搜素框中输入信息的代码程序后,还可以模拟用户的按键操作。
python
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
edge_options = Options()
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver = webdriver.Edge(options=edge_options)
driver.get('http://www.bilibili.com')
driver. find_element(by=By.TAG_NAME,value="input").send_keys("Python"+ Keys.ENTER)
input('dd')

七、案例:实现上传图片
python
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
edge_options = Options()
edge_options.binary_location = r'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe'
driver = webdriver.Edge(options=edge_options)
driver.get('https://graph.baidu.com/pcpage/index?tpl_from=pc')
driver.find_elements(by=By.TAG_NAME, value="input")[1].send_keys(r"C:\Users\Asus\Desktop\e3911e3b07be846cccfe2ed920807e69.png")
#或
#input_element = driver.find_element(by=By.NAME, value="file")
#input_element.send_keys(r"E:\爬虫\img.png")
input('dengdai')

八、浏览器的前进后退
python
from selenium import webdriver
from selenium.webdriver.edge.options import Options
import time
edge_options = Options()
edge_options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver = webdriver.Edge(options=edge_options)
driver.get('http://www.taobao.com') #请求网页http://www.taobao.com
driver.get('http://www.baidu.com')
driver.get('http://www.bilibili.com')
driver.back() #网页返回到上一步
time.sleep(1)
driver.forward()#网页返回到下一步
time.sleep(1)
driver.refresh() #刷新
time.sleep(1)
#driver.quit()#关闭浏览器
driver.close()

九、避坑指南和经验总结
- 驱动 / 版本不匹配:Edge 主版本一致即可(如 144.x),无需小版本完全相同;Selenium4.x 弃用
executable_path,别再混用旧写法。 - 路径报错:指定驱动 / 浏览器路径必加
r;放 Python Scripts 目录后,cmd 输msedgedriver验证环境变量是否生效。 - 元素定位失败:加
time.sleep()或显式等待,避免页面未加载完就定位;优先用ID/XPATH,少用易变的class/tag。 - 滚动 / 获取数据不全:动态加载页面滚动后必须加延时,等待数据渲染;图片 / 链接
src为空时,尝试获取data-src等备用属性。 - 文件上传失效:上传必须定位
<input type="file">元素,其他按钮点击无效,直接用send_keys传本地路径。 - 浏览器操作报错:
close()关当前标签,quit()关整个浏览器;多标签操作需先切换句柄,否则会操作错页面。 - 环境配置:Edge 保持自动更新,驱动放 Python Scripts 目录,一劳永逸;非默认安装 Edge,右键图标复制真实路径配置
binary_location。 - 代码优化:统一封装 Edge 配置(如无头模式
edge_options.add_argument('--headless=new')),避免重复代码;批量操作加异常捕获,防止单个错误中断流程。 - 元素操作:输入 / 点击前先确认元素可见,模拟回车用
Keys.ENTER比点击搜索按钮更稳定;多标签用driver.window_handles切换句柄。 - 数据采集:下载资源时结合
requests,比 Selenium 自带方法更快;动态页面优先用显式等待(WebDriverWait)替代固定time.sleep(),提升执行效率。 - 资源管理:所有操作结束必加
driver.quit(),避免浏览器进程后台残留;爬虫 / 自动化测试加--disable-gpu/--no-sandbox参数,降低资源占用。