Javascript 如何使用 selenium 使用 css 选择器获取所有元素的直接子元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8242169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:17:31  来源:igfitidea点击:

How can I get all element's immediate children with css selectors using selenium?

javascriptcssseleniumwebdriverselenium-webdriver

提问by Shiran

I already tried using the ">" syntax but selenium does not accept it, I know there is a way to get it using Xpath but our entire project is written using CSS selectors.

我已经尝试使用 ">" 语法,但 selenium 不接受它,我知道有一种方法可以使用 Xpath 获取它,但我们的整个项目是使用 CSS 选择器编写的。

I am trying to store a list that will contain all immediate children of an element but not their children (descendants), when I use the "*" syntax I get all the element's descendants.

我试图存储一个列表,该列表将包含一个元素的所有直接子元素,但不包含它们的子元素(后代),当我使用“*”语法时,我得到了所有元素的后代。

回答by jro

You should specify a tag to start from... if you want "all element's immediate children", you would simply get all elements, which isn't really what you want.

您应该指定一个标签开始...如果您想要"所有元素的直接子元素",您只需获取所有元素,这并不是您真正想要的。

To get "all immediate children of an element but not their children"for body, use body > *.

要获得“元素的所有直接子而不是自己的孩子”body,使用body > *

Or another example, to get all direct descendants of <div id='question'>, use div#question > *.

或者另一个示例,要获取 的所有直接后代<div id='question'>,请使用div#question > *