1

I'm trying to create a bot. The functionality I need now is to find a way to get only childs of an element without sub-childs. Take this as example:

<Body>
 <child1>Hi</child1>
 <subChild>Woh</subChild>
 <child2>Hey</child2>
 <subChild>Weee</subChild>
</Body>
</HTML>

I want to find all child elements for but when I do so by:

 childs = Body.find_elements_by_xpath("//*")

It's length by len(childs) is 4 instead of 2. Hope you understood.

Any help will be appreciated.

pavelsaman
4,5581 gold badge15 silver badges37 bronze badges
asked Jun 12, 2020 at 20:48
1
  • * means everything, that won't work. You want to have a look at child. Commented Jun 13, 2020 at 5:42

1 Answer 1

3
childs = Body.find_elements_by_xpath("//*")

You are using xpath "//*" which means any element under the root .

To find direct child element of body use

childs = Body.find_elements_by_xpath("./*") 

Here . , Means current node which is the body

And /* means all direct childs of current node , which is body

answered Jun 13, 2020 at 15:14
1
  • Thanks man! Works perfectly... Commented Jun 13, 2020 at 20:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.