python selenium get child element

Solutions on MaxInterview for python selenium get child element by the best coders in the world

showing results for - "python selenium get child element"
Luisa
08 May 2020
1from selenium import webdriver
2
3driver = webdriver.Firefox()
4driver.get("http://www.stackoverflow.com")
5
6#Here you get the parent element
7header = driver.find_element_by_id("header")
8
9#Here you can get all the child elements by css:
10all_children_by_css = header.find_elements_by_css_selector("*")
11
12#Or here you can get all the child elements by xpath:
13all_children_by_xpath = header.find_elements_by_xpath(".//*")
14
15#And if you just want one element you can take it by index:
16all_children_by_xpath[0] #for first item
17all_children_by_xpath[1] #for the 2