Shreya Agarwal
Shreya Agarwal

Reputation: 716

Extract text from nested attributes of the li element with Selenium and Python

How should I access text from 'strong' and 'span' tags nested under 11 'li' tags in the picture below using Python Selenium?

I'm looking to store the output in dict format: {"Name": Name, Address: No.250/1, 16th and 17th cross..., State: Karnataka, City: Bangalore}

Here's the HTML:

enter image description here

Here's my code:

for elem in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"[id^='arrowex']"))):
    NGO_element = driver.find_element_by_class_name("faq-sub-content exempted-result")
    NGO_name = (driver.find_element_by_class_name("fc-blue fquph")).text.replace(NGO_name_pancard.text, '')
    NGO_name_pancard = driver.find_element_by_class_name("pan-id")
    ul = driver.find_element_by_class_name("exempted-detail")
    for item in (ul.find_elements_by_tag_name("li")):

Upvotes: 0

Views: 991

Answers (1)

Andersson
Andersson

Reputation: 52685

Try below code to get values from strong and span nodes of each li as key-value pair:

data = {}
for item in (ul.find_elements_by_tag_name("li")):
    data[item.find_element_by_tag_name('strong').text] = item.find_element_by_tag_name('span').text

The output of data should looks like {'Address': 'No.250/1, 16th and 17th cross...', 'State': 'Karnataka', 'City': 'Bangalore', etc}

Upvotes: 1

Related Questions