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=}")
相关推荐
介一安全10 小时前
BurpSuite 插件 Log4j2Scan 使用和手动验证
测试工具·log4j·安全性测试
少云清20 小时前
【UI自动化测试】3_web自动化测试 _Selenium-IDE
前端·selenium·web自动化测试
观音山保我别报错2 天前
抽奖项目-接口自动化测试
功能测试·测试工具·单元测试
我的xiaodoujiao2 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 48--本地环境部署Jenkins服务
python·学习·测试工具·pytest
我的xiaodoujiao2 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 49--CI/CD-开始探索使用Jenkins
python·学习·测试工具·ci/cd·jenkins·pytest
学习3人组2 天前
Win11 使用 Proxifier 强制本地流量通过 Fiddler Classic 代理指南
前端·测试工具·fiddler
少云清2 天前
【UI自动化测试】2_web自动化测试 _Selenium环境搭建(重点)
前端·selenium·测试工具·web自动化测试
少云清3 天前
【UI自动化测试】1_web自动化测试 _测试工具选择
测试工具·web自动化测试
小妖6663 天前
有替代postman的软件吗
测试工具·postman
御坂10101号3 天前
Google Ads 转化凭空消失?问题藏在同意横幅的「时机」
前端·javascript·测试工具·网络安全·chrome devtools