selenium – find_element within list of <li> within<ul>

Advertisements
list_of_li = find_elements(by='xpath', value='//ul/*')

list_of_text_i_need = [x.find_element(by='xpath', value='//div/div/article/div/div/div/span').text for x in list_of_li]


This returns the proper number of values len(list_of_li) but every value in list_of_text_i_need is the .text of the very first li in list_of_li not the rest in the first list.

If I print the .text of list_of_li it gives me all the correct values that I’m trying to put into a list but when accessing it with the find_element by xpath it only finds the very first one.

Any help is of course appreciated.

>Solution :

Call find_elements on each element in list_of_li, and get the text of each of those elements with a nested list comprehension.

[y.text for x in list_of_li for y in x.find_elements(by='xpath', value='//div/div/article/div/div/div/span')]

Leave a Reply Cancel reply