java - 从 Selenium 和 JAVA 中的特定标签获取内容
问题描述
我面临以下问题,
考虑这段代码:
<div data-v-2f952c88="" class="text">
<section data-v-3b70ad5b="" data-v-2f952c88="" data-content-provider="OTS" class="description__section">
SOMETHING
</section>
</div>
<div data-v-1343242dd="" class="text">
<section data-v-2232lfd="" data-v-3fgssder="" data-content-provider="HTB" class="description__section">
SOMETING ELSE
</section>
</div>
我如何使用 Selenium 在 Java 中从代码中存在的所有标签“data-content-provider”中提取所有内容,所以最后我可以有一个这样的字符串
字符串结果=“OTS,HTB”。
谢谢
解决方案
// get all section elements
List<WebElement> sections = driver.findElements(By.tagName("section"));
// new empty list of strings
List<String> dataContentProviders = new ArrayList<String>();
// collect all the data-content-provider values to dataContentProviders
for (WebElement section: sections) {
dataContentProviders.add(section.getAttribute("data-content-provider");
}
推荐阅读
- python - 为什么在训练拥抱脸变压器 NER 模型时评估损失会增加?
- postgresql - .Net Core 3.1 和 Postgresql 插入和更新查询未执行,但选择查询运行成功
- netsuite - NetSuite SuiteScript 如何隐藏秘密令牌
- javascript - 导入为变量和连接?
- python - 您可以使用 Python 在 Mac OS 上发送数据包吗?
- terraform - 如何定义“azurerm_resource_group_template_deployment”“parameters_content”部分
- c - 如何在 C 中使用有符号或无符号整数进行位映射?
- python - 如何使用 python 与 Web 服务建立经过身份验证的连接?
- c# - for 循环的问题,并尝试将列表声明从列表字符串到日期时间的转换中分离出来
- python - 从数字列表中创建最大数字的 Python 函数