首页 > 解决方案 > 硒。如何通过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>

标签: javac#selenium

解决方案


下面使用:

String id= driver.findElement(By.xpath("//div[descendant::div/a[@href='https://google.com/']]")).getAttribute("id");
System.out.println(id);

输出

NeedThis

推荐阅读