selenium记录Spiderbuf例题C01

防止自己遗忘,故作此为记录。

步骤:

(1)进入例题,找到需要点击的元素。

可得button xpath

复制代码
click_xpath: str = r'//li/a[@title="mnist"]'
WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, click_xpath)))
res = driver.find_element(By.XPATH, click_xpath)

注意,此时点击res的attribute是完整url。(卡顿在此步,以为url还需要拼接)

之后发现:

可得final_xpath:

复制代码
final_xpath: str = r"//tbody/tr/td[2]"
WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.XPATH, final_xpath)))
res: list = driver.find_elements(By.XPATH, final_xpath)

最后计算:

复制代码
res: list[float] = [eval(e.text) for e in res]
s: Decimal = Decimal("0.0")

for each in res:
    s += Decimal(each)
s /= len(res)

print(f"{s=}")

s2=Decimal('3.766666666666666666666666667')
#四舍五入为3.77

完整代码:

python 复制代码
# -*- coding: utf-8 -*-
# -*- file: C01.py  -*-

from decimal import Decimal
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.chrome.service import Service as ChromeService

first_url: str = r"https://www.spiderbuf.cn/playground/c01"

service = ChromeService(r"C01\chromedriver-win64\chromedriver.exe")
options = ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_argument("--disable-blink-features=AutomationControlled")

driver = Chrome(options=options, service=service)

driver.get(first_url)

click_xpath: str = r'//li/a[@title="mnist"]'
WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, click_xpath)))
res = driver.find_element(By.XPATH, click_xpath)
driver.implicitly_wait(3)

driver.get(res.get_attribute("href"))

#WebDriverWait(driver, 10).until(lambda driver: driver.current_url != first_url)
final_xpath: str = r"//tbody/tr/td[2]"

WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.XPATH, final_xpath)))
res: list = driver.find_elements(By.XPATH, final_xpath)

res = [eval(e.text) for e in res]
length: int = len(res)

s1: float = sum(res) / length

s2: Decimal = Decimal("0.0")

for each in res:
    s2 += Decimal(each)

s2 /= length

print(f"{s1=}", f"{s2=}")

driver.close()
相关推荐
卓码软件测评7 小时前
第三方CNAS软件测试报告【Gatling和云原生环境集成_在Kubernetes和Docker中部署与执行测试】
测试工具·自动化·测试用例·负载均衡·压力测试
程序员勋勋8 小时前
高频Robot Framework软件测试面试题
测试工具·职场和发展
Protein_zmm9 小时前
Wireshark实验一:Web 浏览器与服务器的协议报文捕获与分析
服务器·测试工具·wireshark
阿郎_201113 小时前
python自动化脚本-下载小说
python·selenium·网络爬虫
软件测试雪儿13 小时前
自动化测试面试真题(附答案)
软件测试·测试工具·面试·职场和发展
软件测试雪儿14 小时前
2025年100道最新软件测试面试题,常见面试题及答案汇总
软件测试·测试工具·职场和发展
霍格沃兹测试开发学社16 小时前
被裁后,我如何实现0到3份大厂Offer的逆袭?(内附面试真题)
人工智能·selenium·react.js·面试·职场和发展·单元测试·压力测试
AI绘画小3318 小时前
CTF 逆向一脸懵?这篇攻略把破解技巧讲透,新手也能快速上手!
测试工具·web安全·网络安全
美团测试工程师18 小时前
软件测试面试题2025年末总结
开发语言·python·测试工具
B站计算机毕业设计之家18 小时前
Python+Flask 电商数据分析系统(Selenium爬虫+多元线性回归)商品数据采集分析可视化系统 实时监控 淘宝数据采集 大屏可视化 (附源码)✅
大数据·爬虫·python·selenium·机器学习·flask·线性回归