关于使用Selenium获取网页控制台的数据

背景:

需要获取网页的控制台的数据,如下图

在此文章将使用到 Pycharm 和 Selenium4

Pycharm安装
Selenium安装

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 创建浏览器对象
browser = webdriver.Chrome()


# 打开页面
browser.get("https://www.baidu.com")

# 元素定位 (多种,以下使用XPATH)
# 注:线上大部分的定位函数为find_element_by_id,可能是旧版本Selenium的函数,现使用.find_element(By.___ , "")
browser.find_element(By.XPATH,"//*[@id='kw").send_keys("填入内容") # 填入输入框
browser.find_element(By.XPATH,"//*[@id='su").click() # 点击搜索

##延迟代码结束运行
time.sleep(1000)

关闭浏览器
browser.quit()

Selenium中的find_element方法 //供参考

这里使用Chrome有一个技巧可以直接复制元素的信息

到此我们已经完成了一部分简单的自动化操作了

接下来我们需要记录和获取控制台的信息,这部分参考可参考
jmeter-调用python脚本

python 复制代码
import json
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = Options()

caps = {
    "browserName": "chrome",
    'goog:loggingPrefs': {'performance': 'ALL'}  # 开启日志性能监听
}

# 将caps添加到options中
for key, value in caps.items():
    options.set_capability(key, value)

browser = webdriver.Chrome(options=options)

# 打开页面
browser.get("https://www.baidu.com")
# 填入输入框
browser.find_element(By.XPATH,"//*[@id='kw").send_keys("填入内容") 
# 点击搜索
browser.find_element(By.XPATH,"//*[@id='su").click() 

# 停留10秒加载数据
time.sleep(10)

# 获取名称为 performance 的日志
performance_log = browser.get_log('performance')

# 打印获取的数据,有需要再进一步筛选
print(performance_log)
相关推荐
蒲公英内测分发7 小时前
Typeoff:当 AI 开始参与工作,我们可能需要重新思考“输入”这件事
人工智能·测试工具·项目管理·语音输入·ai语音输入
LT101579744411 小时前
2026年手机机型兼容性测试选型指南:安卓机型碎片化、APP闪退UI错乱如何解决
android·测试工具·ui·智能手机
Luminbox紫创测控14 小时前
GB/T 5137.3 2020标准:汽车安全玻璃耐辐照与耐模拟气候试验方法
人工智能·测试工具·汽车·安全性测试·uv·测试标准
Alan_6911 天前
聊聊 Swagger/Postman/Apifox 的真实选型与 Python 项目实战
python·测试工具·postman
LT10157974441 天前
2026年IE/Chrome/Edge兼容测试选型指南:新旧浏览器内核差异大、后台系统渲染崩溃如何解决
测试工具
icsocket2 天前
芯片测试治具关键组成部分和设计考虑:机械结构中的夹具
测试工具
川石课堂软件测试3 天前
性能测试|Nginx中间件监控与调优
linux·python·nginx·中间件·单元测试·压力测试·harmonyos
标致的自行车3 天前
Selenium 爬虫固定开头:
爬虫·selenium·测试工具
安然无虞4 天前
Python自动化测试·Selenium操控元素的方法
python·selenium·测试工具