pythonapiseleniumweb-scrapingquora

how to click on a webpage all buttons with specific text, one by one


I have this quora webpage as a sample

It does contain 4 "View Upvoters" buttons Using this code, I want to get all 4 button in order to click them one by one later.

upvoter_list = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'AnswerVoterListModalLink')))

print(len(upvoter_list)) gives me 4, which is correct when I print the text of the first element of upvoter_list

print(upvoter_list[0].text) 

I get 'view upvoters' as a result but when I print the remaining, i get the empty result,

print(upvoter_list[1].text)

what is the problem?


Solution

  • Use this (updated)

    from selenium import webdriver 
    from time import sleep
    from bs4 import BeautifulSoup
    obj = webdriver.Chrome('path to driver')
    
    
    list_of_upvoters = []
    obj.get('https://www.quora.com/Have-you-ever-countined-to-pursue-someone-who-wasnt-interested-in-you')
    sleep(5)
    obj.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    sleep(5)
    obj.execute_script("window.scrollTo(0,1);")
    sleep(5)
    
    for p in obj.find_elements_by_class_name('AnswerVoterListModalLink'):
        sleep(5)
        p.click()
        sleep(10)
        for div in obj.find_elements_by_class_name('author_info'):
            list_of_upvoters.append(div.find_element_by_class_name('user').text)
        print(list_of_upvoters)
        list_of_upvoters = []
        sleep(10)
        obj.find_element_by_class_name('modal_close').click()
        sleep(10)