简体   繁体   中英

Selenium python: can't scroll down an overlay window

On Quora, i want to scroll to the bottom of the window i am getting when clicking on view upvoters button, in order to get all upvoters names, the code for scrolling down a standard browser window does not seem to work with an overlay window, any suggestion? Here is my code for scrolldown function and for clicking on 'View upvoters' Button:

def scrolldown(browser):

    src_updated = browser.page_source
    src = ""
    while  src != src_updated:
        src = src_updated
        time.sleep(5)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(5)
        src_updated = browser.page_source      
    #if (DEBUG): print ("window maximized...")
    return browser

browser.get('https://www.quora.com/What-is-it-like-to-regret-having-children')
for p in browser.find_elements_by_class_name('AnswerVoterListModalLink'):
        time.sleep(5)

        p.click()
        time.sleep(5)
        browser=scrolldown(browser)
        time.sleep(5)
        c=0
        for div in upvoter_name :#browser.find_elements_by_class_name('author_info'):
           list_of_upvoters.append(div.find_element_by_class_name('user').text)
           c+=1
        print("number of upvoters for this answer is :" + str(c))

I think you can scroll down to the very last element by using something like in this answer . According to the answer, you can scroll down to the last element with something like this:

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_xpath("//div[@class='pagedlist_item'][last()]")

actions = ActionChains(driver)
actions.move_to_element(element).perform()

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