简体   繁体   中英

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:

在此处输入图片说明

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")):

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM