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=}")
相关推荐
k7Cx7e41 分钟前
Thinkphp6在Postman中接收不到Post参数的解决办法
测试工具·postman
专吃海绵宝宝菠萝屋的派大星43 分钟前
使用postman测试自己编写的mcp服务
测试工具·lua·postman
我的xiaodoujiao5 小时前
API 接口自动化测试详细图文教程学习系列9--Requests模块
python·学习·测试工具·pytest
oi..5 小时前
《Web 安全入门|XSS 漏洞原理、CSP 策略与 HttpOnly 防护实践》
前端·网络·测试工具·安全·web安全·xss
代码的乐趣6 小时前
支持selenium的chrome driver更新到147.0.7727.56
chrome·python·selenium
我的xiaodoujiao6 小时前
API 接口自动化测试详细图文教程学习系列10--Requests模块2--举例说明
python·学习·测试工具·pytest
橘子编程7 小时前
软件测试全流程实战指南
java·功能测试·测试工具·junit·tomcat·压力测试·可用性测试
RunningBComeOn7 小时前
如何通过wireshark抓取802.11无线网络的数据包
网络·测试工具·wireshark
llilian_161 天前
选择北斗导航卫星信号模拟器注意事项总结 北斗导航卫星模拟器 北斗导航信号模拟器
功能测试·单片机·嵌入式硬件·测试工具·51单片机·硬件工程
小白学大数据1 天前
Selenium+Python 爬虫:动态加载头条问答爬取
爬虫·python·selenium