0

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.

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

| improve this answer | |
  • Thanks man! Works perfectly... – Behroz Khan Jun 13 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.