关于使用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)
相关推荐
半路_出家ren7 小时前
流量抓取工具(wireshark)
网络·网络协议·测试工具·网络安全·wireshark·流量抓取工具
猿周LV8 小时前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
西柚小萌新1 天前
【Python爬虫基础篇】--4.Selenium入门详细教程
爬虫·python·selenium
??? Meggie1 天前
Selenium 怎么加入代理IP,以及怎么检测爬虫运行的时候,是否用了代理IP?
爬虫·tcp/ip·selenium
天才测试猿1 天前
软件测试之功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
HtwHUAT1 天前
五、web自动化测试01
前端·css·chrome·python·功能测试·selenium·html
远方2351 天前
应用信息1.13.0发布
测试工具·安全·apk·开发工具·应用·工具·信息
珠峰下的沙砾1 天前
如何在 Postman 中,自动获取 Token 并将其赋值到环境变量
测试工具·lua·postman
桑榆非婉2 天前
同样的接口用postman/apifox能跑通,用jmeter跑就报错500
测试工具·jmeter·postman
苍煜2 天前
Jsoup、Selenium 和 Playwright 的含义、作用和区别
python·selenium·测试工具