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=}")
相关推荐
krack716x4 小时前
Wireshark基本使用
网络·测试工具·wireshark
新时代牛马9 小时前
tcpdump 的用法
网络·测试工具·tcpdump
熊文豪11 小时前
Java+Selenium+快代理实现高效爬虫
java·爬虫·selenium·隧道代理·快代理
水银嘻嘻1 天前
web 自动化之 selenium+webdriver 环境搭建及原理讲解
前端·selenium·自动化
水银嘻嘻1 天前
web 自动化之 Selenium 元素定位和浏览器操作
前端·selenium·自动化
waves浪游2 天前
论坛系统测试报告
测试工具·测试用例·bug·测试
测试老哥2 天前
Selenium使用指南
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
xixixiLucky2 天前
Selenium Web自动化测试学习笔记(一)
笔记·学习·selenium
Lilith的AI学习日记2 天前
纳米AI搜索体验:MCP工具的实际应用测试,撰写报告 / 爬虫小红书效果惊艳
人工智能·测试工具·aigc·ai编程
可了~2 天前
使用 Selenium 截图功能,截不到原生 JavaScript 弹窗
selenium·测试工具