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:


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.

  • * means everything, that won't work. You want to have a look at child.
    – pavelsaman
    Jun 13 '20 at 5:42
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

  • Thanks man! Works perfectly...
    – TheUnknown
    Jun 13 '20 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.