简体   繁体   中英

Selenium : getting next button click content

On Quora website, i am trying to get upvoters names of each answer, by clicking on 'view upvoters', but i am not getting the right results. For example, On this Quora question link you have two answers, first one with 5 upvoters and second one with 2. The result i am getting from the below code is 5,5 .

all_upvotes= browser.find_elements_by_class_name('ExpandedVoterListItem')
for p in all_upvotes:
    p.click()
    time.sleep(10)

    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.modal_content.modal_body'))) 

    upvoter_name = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'span.feed_item_answer_user'))) 

    time.sleep(10)
    print ('number of upvoters found for this answer %d' % len(upvoter_name)) 
    # print upvoters names
    for line in upvoter_name:
           print(line.text)

Looks like you don't select "View Upvoters" link every time, at least in the script you provided. Instead, use the following order of operations:

all_upvotes= browser.find_elements_by_link_text('View Upvoters') # Finds all "View Upvoters" buttons (they are links actually)
for p in all_upvotes:

    p.click() # Opens list of upvoters

    # No need to sleep or wait for anything else, just wait for items you want, namely the name of upvoter (which is again a link):
    upvoter_name = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'a.user')))

    print ('number of upvoters found for this answer %d' % len(upvoter_name)) 

    # print upvoters names
    for line in upvoter_name:
        print(line.text)

    # Now we need to close list of upvoters, to open the next one on next iteration of the loop:
    close_button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'span.modal_close'))) 
    close_button.click()

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