testing - 检查是否可以使用 RSelenium 向下滚动

标签 testing automated-tests rselenium

我正在使用 RSelenium 自动向下滚动社交媒体网站并保存帖子。有时我到达网页底部,由于没有更多数据可用,无法加载更多帖子。我只想检查是否是这种情况,这样我就可以停止尝试滚动。

如何判断是否可以在 RSelenium 中继续滚动?下面的代码说明了我正在尝试做的事情——我想我只需要有关“if”语句的帮助。

仅供引用,在 Python 中有一个解决方案 here (本质上是检查页面高度是否在迭代之间发生变化),但我不知道如何在 R 中实现它(或任何其他解决方案)。

# Open webpage
library(RSelenium)
rD = rsDriver(browser = "firefox")
remDr = rD[["client"]]
url = "https://stocktwits.com/symbol/NZDCHF"
remDr$navigate(url) 

# Keep scrolling down page, loading new content each time. 
ptm = proc.time()
repeat {   
  remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);")
  Sys.sleep(3) #delay by 3sec to give chance to load. 

  # Here's where i need help  
  if([INSERT CONDITION TO CHECK IF SCROLL DOWN IS POSSIBLE]) {
    break
  }
}

最佳答案

偶然发现了一种在 Python 中执行此操作的方法 here并修改它以在 R 中工作。下面是我在上面发布的原始代码的更新。

# Open webpage
library(RSelenium)
rD = rsDriver(browser = "firefox")
remDr = rD[["client"]]
url = "https://stocktwits.com/symbol/NZDCHF"
remDr$navigate(url) 

# Keep scrolling down page, loading new content each time. 
last_height = 0 #
repeat {   
  remDr$executeScript("window.scrollTo(0,document.body.scrollHeight);")
  Sys.sleep(3) #delay by 3sec to give chance to load. 

  # Updated if statement which breaks if we can't scroll further 
  new_height = remDr$executeScript("return document.body.scrollHeight")
  if(unlist(last_height) == unlist(new_height)) {
    break
  } else {
    last_height = new_height
  }
}

关于testing - 检查是否可以使用 RSelenium 向下滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45204934/

相关文章:

java - 如何将原始 html 转换为可以使用 Selenium 测试的内容?

windows - Selenium:无法打开连接:转发新 session 时出错,无法找到 Internet Explorer 的功能

c# - Max()函数的测试用例设计

python - 如何对 python 中的不同实现使用相同的单元测试?

unit-testing - 在 go 中测试 CLI 参数

RSelenium 通过 docker

r - 如何使用 RSelenium 在网页中同时按下两个键(即 control-s)?

r - RSelenium与Docker。 checkError(res)中的错误

testing - TeSTLink 1.9.4 与 bugzilla 4.2.3 集成

javascript - Angular - 测试带有附加集的@Input