Get all child elements
Asked Answered
F

6

133

In Selenium with Python is it possible to get all the children of a WebElement as a list?

Forespent answered 17/7, 2014 at 5:0 Comment(0)
R
200

Yes, you can achieve it by find_elements_by_css_selector("*") or find_elements_by_xpath(".//*").

However, this doesn't sound like a valid use case to find all children of an element. It is an expensive operation to get all direct/indirect children. Please further explain what you are trying to do. There should be a better way.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.stackoverflow.com")

header = driver.find_element_by_id("header")

# start from your target element, here for example, "header"
all_children_by_css = header.find_elements_by_css_selector("*")
all_children_by_xpath = header.find_elements_by_xpath(".//*")

print 'len(all_children_by_css): ' + str(len(all_children_by_css))
print 'len(all_children_by_xpath): ' + str(len(all_children_by_xpath))
Ramunni answered 17/7, 2014 at 5:19 Comment(2)
just figure out that the . before // is important, if the dot is missing, even if you are calling the method from an element, it again search the whole HTMLCreation
Can we use this way to locate a specific child, or the next immediate child? or a whole path, starting from the parent?Abarca
R
94

Yes, you can use find_elements_by_ to retrieve children elements into a list. See the python bindings here: http://selenium-python.readthedocs.io/locating-elements.html

Example HTML:

<ul class="bar">
    <li>one</li>
    <li>two</li>
    <li>three</li>
</ul>

You can use the find_elements_by_ like so:

parentElement = driver.find_element_by_class_name("bar")
elementList = parentElement.find_elements_by_tag_name("li")

If you want help with a specific case, you can edit your post with the HTML you're looking to get parent and children elements from.

Rickrack answered 17/7, 2014 at 5:19 Comment(1)
* find_element_by_class_nameGrassy
G
10

In 2022, with selenium==4.2.0, @Richard's answer will need to be rewritten as:

from selenium.webdriver.common.by import By

parentElement = driver.find_element(By.CLASS_NAME,"bar")
elementList = parentElement.find_elements(By.TAG_NAME,"li")
Glasgow answered 10/10, 2022 at 11:34 Comment(1)
This wont work!Environment
S
7

Another variation of find_elements_by_xpath(".//*") is:

from selenium.webdriver.common.by import By

find_elements(By.XPATH, ".//*")
Sabulous answered 15/6, 2020 at 21:0 Comment(0)
A
2

You can use get_attribute and BeautifulSoup

html_str = el.get_attribute('innerHTML')
bs = BeautifulSoup(html_str, 'lxml')
Aramanta answered 2/10, 2022 at 15:15 Comment(0)
S
1

You can't use

all_children_by_css = header.find_elements_by_css_selector("*")

You now need to use

all_children_by_css = header.find_elements(By.CSS_SELECTOR, "*")
Silvester answered 14/1, 2022 at 0:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.