java - 硒。如何通过href获取div id
问题描述
我有一些 html 代码:
<div id = "NeedThis">
<div style = "width:100%">
<a href = "https://google.com/"></a>
</div>
</div>
如果我知道一个in ,我如何NeedThis
通过 Selenium WebDriver FindElement 和 XPath 或 CssSelector获得一个?语言不重要href
<a>
解决方案
下面使用:
String id= driver.findElement(By.xpath("//div[descendant::div/a[@href='https://google.com/']]")).getAttribute("id");
System.out.println(id);
输出:
NeedThis
推荐阅读
- php - 强制 PHP 以原始格式传递数字
- arrays - 用于编码 Swift 的不同 json 格式
- postgresql - PostgreSQL 查询中的撇号出现(32 位)c3 a2 c2 80
- jquery - 如何使用 jQuery 根据单选按钮值设置选定选项
- java - 黄瓜迁移场景到场景大纲
- c# - 如何使用 Lambda 表达式重写此计算属性?
- google-sheets - 显示最高金额的相应单元格
- python - python-shell和多处理不打印
- python-3.x - 点击图表时如何显示数据?
- javascript - 将 useRef 与 Typescript 一起使用时出错