【问题标题】:Scroll to bottom of infinity scrolling page滚动到无限滚动页面的底部
【发布时间】:2021-10-10 15:50:44
【问题描述】:

我已经阅读了this 的答案,但是,我需要逐步滚动网页,因为如果我直接滚动到文档的高度,则不会加载图像,只有 base64url 中的占位符。 我想滚动一点,直到我到达网页的末尾,确保我可以在驱动程序中看到图像,以便加载它们。

有什么方法可以做到这一点?

我目前的功能:

def scroll_to_bottom(driver):
    last_height = driver.execute_script("return document.body.scrollHeight")
    while True:
        # Scroll down to bottom
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        # Wait to load page
        time.sleep(2)
        # Calculate new scroll height and compare with last scroll height
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            break
        last_height = new_height

这直接进入底部,但如果该项目在过去一段时间内没有聚焦,它将不会加载我需要的图像。

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    您可以通过将下面的代码插入循环来模拟鼠标滚动。

    from pynput.mouse import Controller
    
    mouse = Controller()
    mouse.scroll(0 , -50)
    

    确保先导入pynput 库:

    pip3 install pynput
    

    Watch this short video 更多鼠标模拟。

    【讨论】:

      【解决方案2】:

      您可以尝试如下:

      1:ActionChains的使用:

      from selenium.webdriver.common.action_chains import ActionChains
      
      driver.get("https://shop1236011695.v.weidian.com/?userid=1236011695&spider_token=70ce")
      
      action = ActionChains(driver)
      
      # Create two variables, one for calucuting height before scroll and one for after scroll
      height = 0
      newheight = 1
      
      while height != newheight: # Keep scrolling until the end
          height = driver.execute_script("return document.body.scrollHeight")
          action.send_keys(Keys.END).perform()
          time.sleep(2)
          newheight = driver.execute_script("return document.body.scrollHeight")
          print(height,newheight)
      print("FINISHED SCROLLING")
      
      794 3235
      3235 8866
      8866 21226
      21226 21891
      21891 21891
      FINISHED SCROLLING
      

      2:找到产品并应用scrollIntoView

      i= 0
      try:
          while True:
              options = driver.find_elements_by_xpath("//div[@id='components-wrapper']/div[2]/div")
              driver.execute_script("arguments[0].scrollIntoView(true);",options[i])
              i+=1
              time.sleep(2)
      except Exception as e:
          print(e)
          pass
      

      更新:您也可以使用 Keys.DOWN

      height = 0
      
      while height < 40000: # Keep scrolling until the end
          height += 1000
          action.send_keys(Keys.DOWN).perform()
          time.sleep(2)
          print(height)
      print("FINISHED SCROLLING")
      

      【讨论】:

      • 这样的问题是发送Keys.END会直接滚动到最后,我想做一点滚动,等等等等。
      • @Norhther - 您也可以使用 Keys.DOWN,查看更新后的答案。
      【解决方案3】:

      您可以使用以下代码实现此目的

      document.evaluate('container identifier', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.scrollBy(0,pixel size to scroll)
      

      它一次只会滚动给定的像素大小,然后你可以设置等待或条件来检查元素是否已加载,然后继续。 您也可以使用变量更改像素大小,以便滚动到所需位置。请根据您的需要修改最后一个参数。 另外请将容器标识符替换为实际标识符。

      请尝试以下代码

      def scroll_to_bottom(driver):
              last_height = driver.execute_script("return document.body.scrollHeight")
              while True:
                  # Scroll down to 100 pixels each time
                  self.driver.execute_script("document.evaluate('container identifier', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.scrollBy(0,100)")
                  # Wait to load page
                  time.sleep(2)
                  # Calculate new scroll height and compare with last scroll height
                  new_height = driver.execute_script("return document.body.scrollHeight")
                  if new_height == last_height:
                      break
                  last_height = new_height
      

      【讨论】:

      • @Norhther 你试过这个吗,你的观察结果是什么?
      猜你喜欢
      • 2021-11-10
      • 2012-07-27
      • 1970-01-01
      • 2017-09-06
      • 2011-05-14
      • 1970-01-01
      • 2010-12-25
      相关资源
      最近更新 更多