【问题标题】:Scroll down Followers/Following List in the Instagram Box在 Instagram 框中向下滚动关注者/关注列表
【发布时间】:2018-12-08 10:09:03
【问题描述】:

嗨 :) 我正在寻找一种解决方案来向下滚动 Instagram 框中的以下/关注者列表。 我做的步骤如下:

  • 打开用户 A 的 IG 个人资料;
  • 点击“关注者”按钮;
  • 一个包含 12 个关注者列表的框出现在 IG 框中。

一旦关注者列表显示出来,当我使用该行向下滚动时:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

我知道框下方的页面向下滚动:(

如何在关注者框中向下滚动列表?

在此先感谢 :) 玛丽亚。

【问题讨论】:

    标签: python selenium instagram


    【解决方案1】:

    你可以试试execute_script(),如果是不同的班级,换.isgrP

    ...
    from selenium.webdriver.support.ui import WebDriverWait 
    .....
    # after click follower link, wait until dialog appear
    WebDriverWait(driver, 10).until(lambda d: d.find_element_by_css_selector('div[role="dialog"]'))
    # now scroll
    driver.execute_script('''
        var fDialog = document.querySelector('div[role="dialog"] .isgrP');
        fDialog.scrollTop = fDialog.scrollHeight
    ''')
    

    【讨论】:

    • 太棒了 :) 谢谢...它有效 :) 只是一个澄清。我通过使用 time.sleep(3) 等待,因为使用您的代码会给我返回:WebDriverWait 未定义...即使我导入: from selenium import webdriver from selenium.webdriver.common.keys import Keys
    • 不客气。答案已编辑,添加了所需的导入,如果解决了,请将答案标记为正确。
    • 感谢您的解决方法。我已经尝试解决这个问题 5 个小时了。@ewwink
    【解决方案2】:

    这种方法非常适合我的情况。请不要在循环内更改睡眠时间。它们允许在对话框中重新加载关注者/关注者,而无需向上滚动。

        FList = driver.find_element_by_css_selector('div[role=\'dialog\'] ul')
    numberOfFollowersInList = len(FList.find_elements_by_css_selector('li'))
    
    FList.click()
    actionChain = webdriver.ActionChains(driver)
    time.sleep(random.randint(2,4))
    
    while (numberOfFollowersInList < max):
        actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()        
        numberOfFollowersInList = len(FList.find_elements_by_css_selector('li'))
        time.sleep(0.4)
        print(numberOfFollowersInList)
        actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()            
        time.sleep(1)
    

    【讨论】:

      【解决方案3】:

      您可以测试以下方法。我测试过,它可以工作。

          """Scroll a specific element one or more times with small delay between
          them."""
      
          while times > 0:
              self.driver.execute_script(
                  'arguments[0].scrollTop = arguments[0].scrollHeight',
                  element
              )
              time.sleep(.2)
              times -= 1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-23
        • 1970-01-01
        • 2019-03-07
        相关资源
        最近更新 更多