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=}")
相关推荐
安冬的码畜日常4 小时前
【玩转 Postman 接口测试与开发2_016】第13章:在 Postman 中实现契约测试(Contract Testing)与 API 接口验证(上)
测试工具·postman·契约测试·contract test·postman契约测试
DaisyMosuki6 小时前
selenium记录Spiderbuf例题C01
selenium·测试工具
DaisyMosuki6 小时前
Selenium记录RPA初阶 - 基本输入元件
python·selenium·测试工具
会飞的爱迪生2 天前
mac安装wireshark
测试工具·macos·wireshark
cuijiecheng20182 天前
音视频入门基础:RTP专题(8)——使用Wireshark分析RTP
测试工具·wireshark·音视频
aiweker2 天前
Selenium 使用指南:从入门到精通
python·selenium·测试工具
愈努力俞幸运5 天前
python selenium 用法教程
python·selenium
Future_yzx5 天前
Selenium 浏览器操作与使用技巧——详细解析(Java版)
java·selenium·测试工具
Algorithm-0075 天前
【Postman 接口测试】接口用例设计与评审
测试工具·postman