Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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')]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading