关于使用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)
相关推荐
巴里巴气12 小时前
对selenium进行浏览器和驱动进行配置Windows | Linux
selenium·测试工具
晋阳十二夜17 小时前
【压力测试之_Jmeter链接Oracle数据库链接】
数据库·oracle·压力测试
测试杂货铺20 小时前
Jmeter(六):json断言元件,jmeter参数化实现
jmeter·json
q567315231 天前
Java Selenium反爬虫技术方案
java·爬虫·selenium
有趣的我1 天前
wireshark介绍和使用
网络·测试工具·wireshark
草履虫建模1 天前
Postman - API 调试与开发工具 - 标准使用流程
java·测试工具·spring·json·测试用例·postman·集成学习
龙潜月七1 天前
Selenium 自动化测试中跳过机器人验证的完整指南:能用
python·selenium·机器人
AIZHINAN1 天前
如何评价 selenium 自动化测试框架搭建?
selenium·测试工具
WIN赢11 天前
PostMan使用
测试工具·lua·postman
百里图书12 天前
颠覆传统接口测试!用 Streamlit + SQLite + GPT 打造可视化自动化平台
自动化测试·测试开发·接口自动化测试·测试工具·接口测试·测试平台·python编程