How to get a list of the <li> elements in an <ul> with Selenium using Python?
You need to use the .find_elements_by_
method.
For example,
html_list = self.driver.find_element_by_id("myId")items = html_list.find_elements_by_tag_name("li")for item in items: text = item.text print text
You can use list comprehension:
# Get text from all elementstext_contents = [el.text for el in driver.find_elements_by_xpath("//ul[@id='myId']/li")]# Print textfor text in text_contents: print(text)
Strangely enough, I had to use this get_attribute()-workaround in order to see the content:
html_list = driver.find_element_by_id("myId")items = html_list.find_elements_by_tag_name("li")for item in items: print(item.get_attribute("innerHTML"))