Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

python - Selenium, Scrolling through a dynamic div that change while scrolling

I am trying to make an Instagram bot that gets all followers of a certain account, now I need to scroll to the bottom of the followers box to load all the followers but the box has no unique element at the end, I tried to scroll down using the down key but it says the element is interactable.

def get_followers():
    # click on the followers link
    driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a').click()
    time.sleep(1)
    # the followers box
    scroll_box = driver.find_element_by_xpath('/html/body/div[5]/div/div/div[2]/ul/div')
    action = ActionChains(driver)
    action.move_to_element(scroll_box).click().perform()
    # scrolling to the end to load all element
    while True:
        scroll_box.send_keys(Keys.ARROW_DOWN)


get_followers()

Is there a more sufficient way to scroll through an element. Thanks in Advance.

question from:https://stackoverflow.com/questions/65869766/selenium-scrolling-through-a-dynamic-div-that-change-while-scrolling

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$('[class="isgrP"]').scrollBy(0,350)

find the scroll bar element and scroll that using scrollBy

scrollbar = driver.find_element_by_class_name("isgrP")
driver.execute_script("arguments[0].scrollBy(0,350)", scrollbar) 

or

This will scroll till the end

driver.execute_script("arguments[0].scrollBy(0,arguments[0].scrollHeight)", scrollbar) 

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...