Find tags using css selector but not their descend

2019-02-20 08:23发布

问题:

I'm trying to find tags meeting some condition but just those on the first level (relatively) (not their children etc.). I don't want to find their children, grandchildren etc. whose meeting the condition. Is it possible?

I'm using Selenium with Python

<div id="example1">
    <div id="example2">
    </div>
</div>
<div id="example3">
</div>

I want to return example1 and example3 (not example2).

Those divs can be a children of a body but they can be also grandchildren etc. so driver.find_element_by_css_selector(body > div[name*=example]) is not enough.

回答1:

Something like this could do the trick:

*:not([id*=example]) > [id*=example]

http://codepen.io/anon/pen/EVxYjE