首页 > 解决方案 > Selenium,滚动浏览在滚动时发生变化的动态 div

问题描述

我正在尝试制作一个 Instagram 机器人来获取某个帐户的所有关注者,现在我需要滚动到关注者框的底部以加载所有关注者,但该框最后没有唯一元素,我试图向下滚动使用向下键,但它表示该元素是可交互的。

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()

是否有更充分的方式来滚动元素。提前致谢。

标签: pythonseleniumselenium-webdriver

解决方案


$('[class="isgrP"]').scrollBy(0,350)

找到滚动条元素并使用 scrollBy 滚动它

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

或者

这将滚动到最后

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

在此处输入图像描述


推荐阅读