fetching selenium data and appendinig into dictionary {key:value} pair in python

enter image description hereplease refer the attached picture image. I am trying fetch the selenium data and trying to append the output in Dictionary.As shown below in the expected output :

from selenium import webdriver
PATH ="/home/sunbeam/chromedriver"
driver = webdriver.Chrome(PATH)

driver.get("https://trends24.in/india/")
print(driver.title)

videos1=driver.find_element_by_class_name('page-content__trend-list').text
print(videos1)

driver.quit()

Output:

54 minutes ago
#CSKvDC
66K
Raina
36K
Dhoni
39K
Dhoni
39K   etc...

Expected Output:

   54 minutes ago

   [{"#CSKvDC":"66K"},
   {"Raina":"36K"},
   {"Dhoni":"39K"},
   {"Dhoni":"39K"}] etc...

Answer

driver.get("https://trends24.in/india/")
print(driver.title)
values=[]
first=driver.find_element_by_xpath("(//div[@class='trend-card'])[1]")
time=first.find_element_by_xpath("./h5[@class='trend-card__time']").text
print(time)
for li in first.find_elements_by_xpath("./ol[@class='trend-card__list']/li"):
    arr=li.text.split('n')
    #print(arr)
    title=arr[0]
    if len(arr)==2:
        tweetCount = arr[1]
    else:
        tweetCount= "NA"
    values.append({title:tweetCount})
print(values)

Create an empty list for your values and first print time out. Then loop and split the values and put the title in the first and tweetCount in the second. Some values don’t have tweetCounts so I just used NA for the value.

Outputs

India | Twitter trending hashtag and topics today | trends24.in
56 minutes ago
[{'#WrestleMania': '216K'}, {'#ElClasico': '397K'}, {'Barca': '264K'}, {'#HalaMadrid': '116K'}, {'Messi': '299K'}, {'#SusuJournalists': 'NA'}, {'Zidane': '89K'}, {'#CSKvDC': '117K'}, {'Benzema': '233K'}, {'Valverde': '39K'}]

Basically replace first with trendCard(any variable)

driver.get("https://trends24.in/india/")
print(driver.title)

for trendCard in driver.find_elements_by_xpath("//div[@class='trend-card']"):
    values=[]
    time=trendCard.find_element_by_xpath("./h5[@class='trend-card__time']").text
    print(time)

    for li in trendCard.find_elements_by_xpath("./ol[@class='trend-card__list']/li"):
        arr=li.text.split('n')
        # print(arr)
        title=arr[0]
        if len(arr)==2:
            tweetCount = arr[1]
        else:
            tweetCount= "NA"
        values.append({title:tweetCount})
    print(values)

Outputs:

India | Twitter trending hashtag and topics today | trends24.in
50 minutes ago
[{'#DearSidNaaz': '387K'}, {'#WhoCreatedTheUniverse': '393K'}, {'#Justice4ShambhuPujari': '40K'}, {'#SecretsOfNature': '344K'}, {'Rhea Planted In SSR Life': '69K'}, {'#JyotibaPhule': '16K'}, {'टीका उत्सव': '27K'}, {'Gill': '140K'}, {'super woman promo': 'NA'}, {'Rashid Khan': 'NA'}]
1 hour ago
[{'#DearSidNaaz': '127K'}, {'#WhoCreatedTheUniverse': '368K'}, {'#SecretsOfNature': '322K'}, {'#Justice4ShambhuPujari': '31K'}, {'#jyotibaphule': '14K'}, {'Rhea Planted In SSR Life': '54K'}, {'टीका उत्सव': '23K'}, {'सुंदर शब्द': 'NA'}, {'Gill': '77K'}, {'Super Woman Promo': 'NA'}]
2 hours ago
[{'#WhoCreatedTheUniverse': '343K'}, {'#SecretsOfNature': '301K'}, {'#sundayvibes': 'NA'}, {'#Justice4ShambhuPujari': '17K'}, {'#jyotibaphule': '10K'}, {'Rhea Planted In SSR Life': '40K'}, {'सुंदर शब्द': 'NA'}, {'टीका उत्सव': '18K'}, {'Satlok': '96K'}, {'God Kabir': '84K'}]
3 hours ago
[{'#WhoCreatedTheUniverse': '310K'}, {'#SecretsOfNature': '272K'}, {'#sundayvibes': 'NA'}, {'#jyotibaphule': 'NA'}, {'#मोदी_नंग_किसान_तंग': '53K'}, {'Rhea Planted In SSR Life': '24K'}, {'Satlok': '89K'}, {'God Kabir': '78K'}, {'सामाजिक क्रांति': 'NA'}, {'महान समाज': '11K'}]
4 hours ago
[{'#WhoCreatedTheUniverse': '266K'}, {'#SecretsOfNature': '235K'}, {'#मोदी_संग_किसान': '12K'}, {'#sundayvibes': 'NA'}, {'#मोदी_नंग_किसान_तंग': '41K'}, {'Satlok': '82K'}, {'God Kabir': '70K'}, {'Rhea Planted In SSR Life': '10K'}, {'कबीर परमेश्वर': '22K'}, {'Jyoti Niranjan': 'NA'}]
5 hours ago
[{'#WhoCreatedTheUniverse': '206K'}, {'#SecretsOfNature': '183K'}, {'#मोदी_नंग_किसान_तंग': '27K'}, {'#DAP_खाद_की_दर_कम_करो': '17K'}, {'#SpiritualSunday': '12K'}, {'Satlok': '71K'}, {'God Kabir': '59K'}, {'Jyoti Niranjan': 'NA'}, {'कबीर परमेश्वर': '18K'}, {'ज्योति निरंजन': '14K'}]
6 hours ago
[{'#WhoCreatedTheUniverse': '104K'}, {'#SecretsOfNature': '95K'}, {'#SpiritualSunday': 'NA'}, {'#DAP_खाद_की_दर_कम_करो': '11K'}, {'Satlok': '51K'}, {'#मोदी_नंग_किसान_तंग': '13K'}, {'God Kabir': '40K'}, {'Jyoti Niranjan': 'NA'}, {'कबीर परमेश्वर': '10K'}, {'ज्योति निरंजन': 'NA'}]
7 hours ago
[{'#WrestleMania': '296K'}, {'#DAP_खाद_की_दर_कम_करो': 'NA'}, {'#sundayvibes': 'NA'}, {'#ElClasico': '401K'}, {'#SundayMorning': 'NA'}, {'Lashley': '16K'}, {'Dembele': '69K'}, {'Barca': '266K'}, {'Sushant Ki Aawaz Arnab': '99K'}, {'Drew': '86K'}]
8 hours ago
[{'#WrestleMania': '216K'}, {'#ElClasico': '397K'}, {'Barca': '264K'}, {'#HalaMadrid': '116K'}, {'Messi': '299K'}, {'#SusuJournalists': 'NA'}, {'Zidane': '89K'}, {'#CSKvDC': '117K'}, {'Benzema': '233K'}, {'Valverde': '39K'}]
8 hours ago
[{'#ElClasico': '392K'}, {'Barca': '261K'}, {'#HalaMadrid': '113K'}, {'Dembele': '67K'}, {'#CSKvDC': '117K'}, {'#susujournalists': 'NA'}, {'Messi': '295K'}, {'Koeman': '54K'}, {'Benzema': '228K'}, {'Zidane': '86K'}]
9 hours ago
[{'#ElClasico': '385K'}, {'Barca': '257K'}, {'#HalaMadrid': '107K'}, {'Dembele': '66K'}, {'#CSKvDC': '117K'}, {'#SusuJournalists': 'NA'}, {'Messi': '289K'}, {'Casemiro': '51K'}, {'Koeman': '50K'}, {'Zidane': '81K'}]
10 hours ago
[{'#ElClasico': '363K'}, {'Barca': '245K'}, {'#HalaMadrid': '93K'}, {'Dembele': '63K'}, {'#CSKvDC': '116K'}, {'Messi': '273K'}, {'#SusuJournalists': 'NA'}, {'Casemiro': '45K'}, {'Koeman': '41K'}, {'Benzema': '202K'}]
11 hours ago
[{'#ElClasico': '304K'}, {'#CSKvDC': '116K'}, {'Dembele': '56K'}, {'Barca': '207K'}, {'#SusuJournalists': 'NA'}, {'#RealMadridBarcelona': '39K'}, {'Messi': '224K'}, {'Sushant Ki Aawaz Arnab': '85K'}, {'#DelhiCapitals': '15K'}, {'Casemiro': '34K'}]
12 hours ago
[{'#ElClasico': '245K'}, {'#CSKvDC': '115K'}, {'Dembele': '50K'}, {'#SusuJournalists': 'NA'}, {'#DelhiCapitals': '15K'}, {'Barca': '173K'}, {'Sushant Ki Aawaz Arnab': '80K'}, {'Shaw': '25K'}, {'Benzema': '136K'}, {'#CRYCHE': '66K'}]
13 hours ago
[{'#CSKvDC': '114K'}, {'#DelhiCapitals': '14K'}, {'Shaw': '24K'}, {'#SusuJournalists': 'NA'}, {'#CRYCHE': '63K'}, {'Sushant Ki Aawaz Arnab': '73K'}, {'Dhoni': '57K'}, {'#ElClasico': '158K'}, {'Pulisic': '37K'}, {'Havertz': '48K'}]
14 hours ago
[{'#CSKvDC': '109K'}, {'#DelhiCapitals': '14K'}, {'Shaw': '23K'}, {'Dhoni': '54K'}, {'#CRYCHE': '50K'}, {'Sushant Ki Aawaz Arnab': '61K'}, {'#BANGBANGCON21': '365K'}, {'#ThankYouCRPAATIL': 'NA'}, {'Raina': '50K'}, {'Pulisic': '25K'}]
15 hours ago
[{'#CSKvDC': '97K'}, {'Shaw': '20K'}, {'#BANGBANGCON21': '337K'}, {'Dhoni': '49K'}, {'Raina': '47K'}, {'#CRYCHE': '38K'}, {'#AskStar': 'NA'}, {'#DelhiCapitals': '11K'}, {'Sushant Ki Aawaz Arnab': '43K'}, {'Sam Curran': '12K'}]
16 hours ago
[{'#CSKvDC': '87K'}, {'Raina': '45K'}, {'#DhoniReturns': '17K'}, {'#BANGBANGCON21': '293K'}, {'#ChinnaThala': 'NA'}, {'Sam Curran': '11K'}, {'#LIVAVL': '36K'}, {'Shaw': '13K'}, {'Mr. IPL': 'NA'}, {'Sushant Ki Aawaz Arnab': '23K'}]
17 hours ago
[{'#CSKvDC': '66K'}, {'Raina': '36K'}, {'Dhoni': '39K'}, {'Dhoni': '39K'}, {'#BANGBANGCON21': '193K'}, {'Mr. IPL': 'NA'}, {'#WhistlePodu': '97K'}, {'#YSJaganFearsVakeelSaab': '130K'}, {'#Askstar': 'NA'}, {'Avesh Khan': 'NA'}]
17 hours ago
[{'#CSKvDC': '43K'}, {'#YSJaganFearsVakeelSaab': '105K'}, {'#WelcomeDhoni': '21K'}, {'#MCILEE': '31K'}, {'Leeds': '104K'}, {'ICONIC MSD RETURNS': '18K'}, {'Raina': '16K'}, {'#WhistlePodu': '85K'}, {'Dallas': '61K'}, {'CEEDED EMPEROR RAMCHARAN': '27K'}]
18 hours ago
[{'#CSKvsDC': '19K'}, {'#MCILEE': '26K'}, {'#WelcomeDhoni': '18K'}, {'#YSJaganFearsVakeelSaab': '78K'}, {'Leeds': '83K'}, {'ICONIC MSD RETURNS': '16K'}, {'#TemplesOnlyForHindus': '44K'}, {'Dallas': '54K'}, {'seokjin': '339K'}, {'Bielsa': '22K'}]
19 hours ago
[{'#YSJaganFearsVakeelSaab': '27K'}, {'#WelcomeDhoni': 'NA'}, {'#TemplesOnlyForHindus': '36K'}, {'ICONIC MSD RETURNS': 'NA'}, {'seokjin': '310K'}, {'#CSKvsDC': '14K'}, {'#BreakTheTwitterBeard': 'NA'}, {'CEEDED GADDA NTR ADDA': '11K'}, {'1M IG POSTS ADMIRING ALY': '52K'}, {'Sterling': '30K'}]
20 hours ago
[{'#TemplesOnlyForHindus': '23K'}, {'seokjin': '272K'}, {'#SwiggyForkCast': 'NA'}, {'#BreakTheTwitterBeard': 'NA'}, {'#CSKvDC': '18K'}, {'#VikrantRonaAnnouncement': '25K'}, {'1M IG POSTS ADMIRING ALY': '46K'}, {'CEEDED GADDA NTR ADDA': 'NA'}, {'Cooper Botched SSR Case': '117K'}, {'bogum': '18K'}]
21 hours ago
[{'#TemplesOnlyForHindus': 'NA'}, {'#Clubhouse': '45K'}, {'#VikrantRonaAnnouncement': '10K'}, {'#CSKvsDC': '10K'}, {'#BreakTheTwitterBeard': 'NA'}, {'1M IG POSTS ADMIRING ALY': '38K'}, {'Cooper Botched SSR Case': '106K'}, {'bogum': 'NA'}, {'CISF': 'NA'}, {'Krishnanagar': 'NA'}]