selenium记录Spiderbuf例题C03

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

鸢尾花数据集(Iris Dataset)

这道题牵扯到JS动态加载。

步骤:

(1)进入例题,需要找到按钮规律。

复制代码
flip_xpath: str = r"//li/a[@onclick='getIrisData({});']"

(2)找到 Sepal Width列的XPATH:

复制代码
target_xpath: str = r"//tbody/tr/td[3]"

(3)这道题比C01、C02的星级都高的原因我觉得是:

JS动态加载需要极大时间,不然网站反应不过来。

所以只能用sleep来等待了。


源代码:

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

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.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from time import sleep


target_url: str = r"https://www.spiderbuf.cn/playground/c03"
service: object = ChromeService(r"c03\chromedriver-win64\chromedriver.exe")
options: object = ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_argument("--disable-blink-features=AutomationControlled")

driver: object = Chrome(service=service, options=options)
driver.get(target_url)

target_xpath: str = r"//tbody/tr/td[3]"
flip_xpath: str = r"//li/a[@onclick='getIrisData({});']"
s: float = 0.0
page: int = 1

while page < 6:
    page += 1
    sleep(1)
    WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.XPATH, target_xpath)))
    res: list = driver.find_elements(By.XPATH, target_xpath)
    print("time: {}".format(page), res[0].text, res[1].text)
    s += sum([eval(each.text) for each in res])
    try:
        driver.find_element(By.XPATH, flip_xpath.format(page)).click()
    except:
        break
    sleep(5)


print(f"{s=}")
相关推荐
泽众云测试14 小时前
2026年自动化测试平台对比:以TestOne为主与国外主流商用平台分析
功能测试·测试工具·自动化测试平台
Luminbox紫创测控15 小时前
车载HUD阳光倒灌测试:GB/T46926标准的太阳光模拟器方案
测试工具·汽车·安全性测试·测试标准
随风而飘18615 小时前
罗德与施瓦茨(R&S)SMB100B中端高性能模拟射频/微波信号发生器
网络·功能测试·科技·测试工具
我的xiaodoujiao18 小时前
使用PyMySQL模块技术操作MySQL数据库
数据库·python·测试工具·mysql
摸摸芋2 天前
postman(ai篇)
测试工具·postman
St_rive2 天前
selenium元素定位方法
selenium·测试工具
St_rive2 天前
selenium常用等待方式
selenium·测试工具
随风而飘1862 天前
罗德与施瓦茨 FSP13 中高端频谱分析仪
网络·功能测试·测试工具
测试狗科研平台2 天前
从实验室到量产:测试狗新材料中试的技术路径与平台支撑
测试工具·服务发现·材料工程