javascript - 如何在selenium javascript中遍历具有特定类名的元素
问题描述
页面上有动态数量的 div 容器,类名为“海绵”,我需要遍历这些容器,然后从每个容器中获取详细信息,但我不知道该怎么做。这是我到目前为止尝试过的......
const containers = await driver.findElements(By.className(`sponge`))
for(let t = 0; t < containers.length; t++){
let runner = await containers[t].findElement(By.xpath(`//div[2]/div[1]/div/p`)).getText()
console.log(runner)
}
当我这样做时,我只是在第一个“海绵”类容器中获得“跑步者”x 次,其中 x 是“容器”的长度。它实际上并没有找到我想要的容器中的“跑步者”
解决方案
要立即child node
获得parent node
您需要使用的立即.
尝试。
const containers = await driver.findElements(By.className("sponge"))
for(let t = 0; t < containers.length; t++){
let runner = await containers[t].findElement(By.xpath(".//div[2]/div[1]/div/p")).getText()
console.log(runner)
}
推荐阅读
- node.js - 重新连接后,Mongoose 连接计数到 MongoDB Atlas
- android - Android Dark 主题反转矢量绘图的颜色
- python - 存储多个 XGBoost 模型
- c# - 单个查询中来自多个表的多个计数 - EF Core、LINQ
- javascript - 在 Picker 视图中显示两个 Array 项
- kotlin - MutableStateFlow 强制更新/通知收集器
- ubuntu - 在 Ubuntu Server 20.04 中安装后无法访问 Nextcloud Web 界面
- c++ - 了解在不使用乘法运算符的情况下实现 square() 的解决方案
- php - Laravel Livewire 路由不会显示正确的刀片
- python - 反转matplotlib TimedAnimation图中的x刻度标签,而不反转数据?