使用RSelenium滚动到长动态页面的末尾

使用RSelenium滚动到长动态页面的末尾,r,parsing,scroll,rselenium,R,Parsing,Scroll,Rselenium,我知道了如何使用RSelenium滚动动态页面,但是页面的长度每次都不同,因此有时对于包含许多元素的长页面,我的代码无法工作,并且在到达页面底部之前停止。有人知道如何避免吗? 提前谢谢你 我的代码示例: 第一个很好用,可以这样滚动到页面末尾。我不应该以某种方式定义特定的卷轴数,比如10个,而是指定重复这种卷轴直到某个时刻的条件。但是我不知道怎么 for(i in 1:10){ cd$executeScript(paste("scroll(0,",i*10000,");")) S

我知道了如何使用RSelenium滚动动态页面,但是页面的长度每次都不同,因此有时对于包含许多元素的长页面,我的代码无法工作,并且在到达页面底部之前停止。有人知道如何避免吗? 提前谢谢你

我的代码示例: 第一个很好用,可以这样滚动到页面末尾。我不应该以某种方式定义特定的卷轴数,比如10个,而是指定重复这种卷轴直到某个时刻的条件。但是我不知道怎么

    for(i in 1:10){ 
  cd$executeScript(paste("scroll(0,",i*10000,");"))
  Sys.sleep(3)    
}

###after 10 big scrolls starts mini scrolls like that###
element <- cd$findElement("css", "body")
flag <- TRUE
counter <- 0
n <- 5
while(flag){
  counter <- counter + 1
  #compare the pagesource every n(n=5) time, since sometimes one scroll down doesn't render new content
  for(i in 1:n){
    element$sendKeysToElement(list("key"="page_down"))
    Sys.sleep(2)
  }
  if(exists("pagesource")){
    if(pagesource == cd$getPageSource()[[1]]){
      flag <- FALSE
      writeLines(paste0("Scrolled down ",n*counter," times.\n"))
    } else {
      pagesource <- cd$getPageSource()[[1]]
    }
  } else {
    pagesource <- cd$getPageSource()[[1]]
  }
} #But unfortunately this line is also powerless before the real big pages so it stops before reaching the bottom
for(1:10中的i){
cd$executeScript(粘贴(“滚动(0,,i*10000,);”)
系统睡眠(3)
}
###10个大卷轴之后,开始像那样的小卷轴###
要素