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


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.

Asked By: lexlikely



Call find_elements to get all matching elements (rather than find_element which only returns one) 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')]
Answered By: Unmitigated
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.