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()
相关推荐
北岛三生4 小时前
Camera tuning flow相机调试流程
图像处理·数码相机·测试工具·模块测试
晋人在秦 老K9 小时前
入梦工具箱怎么检测硬件?3步完成CPU-Z跑分测试 硬件检测总出错?图吧工具箱免费功能实测 draw.io 部署指南:私有化流程图服务搭建教程
测试工具·流程图·工具·draw.io
zhangzeyuaaa19 小时前
Selenium 超时完全指南:pageLoadTimeout、implicitlyWait 和 scriptTimeout 的深度解析
selenium·测试工具
泛联新安1 天前
如何根据项目需求选择合适的软件测试工具?iUnit智能单元测试平台提供专业化解决方案
c++·测试工具·单元测试
c萱2 天前
软件测试错题笔记
软件测试·数据库·笔记·测试工具·oracle·测试用例
测试开发Kevin3 天前
详解Grafana k6 的阈值(Thresholds)
测试工具·压力测试
kebeiovo3 天前
常用的几种测试工具:selenium,jmeter,jenkins
selenium·测试工具·jmeter
小白学大数据3 天前
应对反爬:使用Selenium模拟浏览器抓取12306动态旅游产品
selenium·测试工具·旅游
程序员的世界你不懂3 天前
【框架】基于selenium+java框架设计(0-1实战)
java·selenium·servlet