Python实现浏览器自动化

本文最后更新于 2025年7月11日 下午

0、简介 ✨

Selenium 可以在无人参与的情况下自动操作浏览器,通过代码模拟用户行为,实现自动化流程,比如输入文本、提交表单等。

1、安装 🛠️

  1. 安装 selenium 库
1
pip3 install selenium
  1. 查看浏览器版本(确保驱动匹配)

image-20240716144724361

  1. 下载对应浏览器驱动

下载与你浏览器版本匹配的驱动文件,放到 Python 项目根目录。

image-20240716144545993
image-20240716144857117

2、演示代码 💻

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

# 定义元素定位
input_location = (By.XPATH, '//*[@id="ww"]')
button_location = (By.XPATH, '//*[@id="s_btn_wr"]')
news_location = (By.XPATH, '//*[@id="1"]/div/h3/a')

# 初始化 Chrome 浏览器参数
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito") # 隐身模式
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"]) # 关闭自动化和日志信息

# 创建浏览器实例
browser = webdriver.Chrome(options=chrome_options)
browser.get("https://news.baidu.com/") # 打开网页
browser.maximize_window() # 最大化窗口
browser.implicitly_wait(5) # 隐式等待

# 操作页面元素
browser.find_element(*input_location).send_keys("熊猫") # 输入搜索关键词
browser.find_element(*button_location).click() # 点击搜索按钮
text = browser.find_element(*news_location).text # 获取第一条新闻标题
print(text)

time.sleep(3) # 等待3秒
browser.quit() # 关闭浏览器

IMG\_20240716150127

3、参数扩展及优化 ⚙️

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
初始化 Chrome 浏览器配置
"""
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito") # 隐身模式
# chrome_options.add_argument('--headless') # 无界面模式(取消注释即可开启)
chrome_options.add_argument("--disable-cache") # 禁用缓存
chrome_options.add_argument("--disable-plugins") # 禁用插件
chrome_options.add_argument("--disable-notifications") # 禁用通知
chrome_options.add_argument("--disable-component-update")# 禁用组件更新
chrome_options.add_argument("--ignore-certificate-errors") # 忽略证书错误
chrome_options.add_experimental_option("useAutomationExtension", False) # 禁用自动化扩展
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"]) # 关闭日志

browser = webdriver.Chrome(options=chrome_options)
browser.maximize_window()
browser.implicitly_wait(5)

"""
初始化 Firefox 浏览器配置
"""
from selenium.webdriver.firefox.options import Options

firefox_options = Options()
# firefox_options.add_argument("--private") # 隐私模式
firefox_options.set_preference("app.update.auto", False) # 禁用自动更新
firefox_options.set_preference("app.update.enabled", False)
firefox_options.set_preference("browser.ssl_override_behavior", 1) # 忽略证书错误
firefox_options.set_preference("dom.webnotifications.enabled", False) # 禁用通知
firefox_options.set_preference("browser.dom.window.dump.enabled", False) # 禁用窗口日志
firefox_options.set_preference("browser.console.showInPanel", False) # 禁用控制台日志
firefox_options.set_preference("toolkit.telemetry.reportingpolicy.firstRun", False) # 禁用首次运行报告
# firefox_options.headless = True # 无界面模式

browser = webdriver.Firefox(options=firefox_options)
browser.maximize_window()
browser.implicitly_wait(5)

如果你想快速上手浏览器自动化,Selenium 是非常实用的利器!💪
在日常任务、测试自动化、爬虫等场景都能派上用场~