如何使用selenium使用css选择器获取所有元素的直接子项? [英] How can I get all element's immediate children with css selectors using selenium?

查看:570
本文介绍了如何使用selenium使用css选择器获取所有元素的直接子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

你应该指定一个标签来开始...如果你想要所有元素的直接孩子,你会简单地得到所有元素,这不是你想要的。

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.

要获得元素的所有直系子元素而不是它们的孩子 正文,使用正文> *

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

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

这篇关于如何使用selenium使用css选择器获取所有元素的直接子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆