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()
相关推荐
LT10157974449 小时前
2026年低代码自动化测试平台选型指南:降低测试落地门槛
测试工具·低代码·自动化
llilian_169 小时前
晶振测量仪 晶振频率测试仪器的多领域应用解析 晶振频率测试仪器
功能测试·单片机·嵌入式硬件·测试工具·51单片机
西安同步高经理1 天前
B码发生器破解变电站故障录波的时间困局、b码同步时钟、b码授时
测试工具
大貔貅喝啤酒1 天前
接口测试_Postman(详细版)
javascript·测试工具·node.js·自动化·postman
Air_July1 天前
Brower User Web UI部署详细步骤
人工智能·python·测试工具
测试19981 天前
接口测试工具:Postman的高级用法
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
汽车仪器仪表相关领域1 天前
Kvaser Memorator Professional HS/LS:高速 + 低速双通道 CAN 总线记录仪,跨系统诊断的专业级解决方案
网络·人工智能·功能测试·测试工具·安全·压力测试
程序员小远1 天前
如何编写测试用例?
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
PhotonixBay2 天前
激光共聚焦显微镜的非接触式原位表面表征测量
人工智能·测试工具