将屏幕截图保存为R Selenium中的变量

时间:2017-06-08 23:05:44

标签: r selenium imagemagick

我正在尝试将Selenium的屏幕截图存储到变量中,以便我可以计算出它的高度:

library(RSelenium)
library(magick)
server <- remoteDriver(remoteServerAddr = "localhost",
                      port = 4444,
                      browserName = "firefox")
server$open()
server$navigate("https://google.com")
screenshot <- server$screenshot(display = FALSE)
image_info(screenshot)
  

错误:'image'参数不是magick图像对象。

奇怪的是,如果我将server $ screenshot输出到一个文件并将其加载回来:

,这是有效的
server$screenshot(display = FALSE, file"/home/person/img.png")
blankPNG <-  image_read("/home/person/img.png")

know服务器$ screenshot返回“base64编码的PNG”,但我怎么能这样读? print(screenshot)返回一个字符串。

1 个答案:

答案 0 :(得分:2)

终于找到了它:

library('base64enc')

# this returns a list of base64 characters
screenshot <- server$screenshot(display = FALSE)

# converts the base64 characters into a vector
screenshot <- base64decode(toString(screenshot), output = NULL)

# reads the vector as stores it as a PNG
screenshot <- image_read(screenshot)