硒.如何通过href获取div id



我有一个html代码:

<div id = "NeedThis">
<div style = "width:100%">
<a href = "https://google.com/"></a>
</div>
</div>

如果我知道<a>中的href,我如何通过Selenium WebDriver FindElement和XPath或CsSelector获得NeedThis?语言无关紧要

使用以下内容:

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

输出

NeedThis

最新更新