How to get list of <li> elements in <ul> with Selenium using Python?

I am using Selenium WebDriver using Python tests for UI and I want to check the following HTML:

<ul id="myId"> <li>Something here</li> <li>And here</li> <li>Even more here</li> </ul> 

From this unordered list, I want to iterate over the elements and check the text in them. I chose ul-element as my id , but I cannot find any way to loop over <li> -children in Selenium.

Does anyone know how you can scroll through an <li> -childeren unordered list with Selenium (in Python)?

+6
source share
2 answers

You need to use the .find_elements_by_ method.

For instance,

 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 
+13
source

You can use list comprehension:

 # Get text from all elements text_contents = [el.text for el in driver.find_elements_by_xpath("//ul[@id='myId']/li")] # Print text for text in text_contents: print text 
0
source

Source: https://habr.com/ru/post/982221/


All Articles