Find the div with class "bar" whose ancestor does not have class "foo" within the scope of top-level "foo". For example,
<div class="foo">
<div class="foo">
<div><div>
<div class="bar"> </div>
</div></div>
</div>
<div class="bar"> </div>
</div>
Find the bar not inside the nested "foo".
And the XPath I tried is:
WebElement root = driver.findElement(By.xpath("//div[@class='foo']"));
root.findElements(By.xpath(".//div[not(contains(@class, 'foo'))]//div[contains(@class, 'bar')]"));
But the XPath returns two elements. Also tried css selector:
root.findElements(By.cssSelector(":not(.foo) .bar"));
Not working either.
Try this XPath-1.0 expression:
//div[contains(@class, 'bar') and not(ancestor::*[@class='foo'])]
It only returns one item: the desired <div class="bar"> </div>
.
这个只返回第二个栏(至少当我运行它时......)
//*[@class='bar'][(parent::div[@class='foo'])]
如果您尝试使用 bar 类访问直接子 div,那么这里是 xpath。
//div[@class='bar' and parent::div[@class='foo']]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.