关于使用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)
相关推荐
微软技术分享10 小时前
BinSentry 自研二进制哨兵压力测试+冒烟回归脚本
压力测试
测试员周周1 天前
【jev应用到测试】94 条 UI 报错交给 Jev,三档判对 85.1%,开单闸门定在 0.6
人工智能·python·测试工具·单元测试·测试用例
xfilm埃利测量1 天前
从方阻到电阻率:四探针法如何把薄膜“测明白“
功能测试·测试工具·四探针·薄膜电阻测量
禹凕1 天前
机器学习之Selenium(Machina Learning about Selenium)
爬虫·python·selenium·测试工具·机器学习
程序媛_2 天前
【JMeter】准备token文件
android·jmeter
水水不水啊3 天前
告别杂乱的调试窗口:我用 Python + WebView 写了一个现代化串口助手
python·测试工具·嵌入式·嵌入式开发·串口调试·串口助手
把头塞进显示器10 天前
电商平台-测试报告
python·selenium·pytest
霍格沃兹测试学院-小舟畅学11 天前
UI 测试的语义断言:Midscene aiAssert 比像素比对稳在哪、又会骗在哪
人工智能·测试工具
zuozewei11 天前
附录 A:主流工具对比选型表
人工智能·测试工具
我的xiaodoujiao11 天前
Django 基础知识详细图文教程 10-Django 模板引擎 3
后端·python·测试工具·django