Rselenium - 将页面另存为

时间:2017-08-29 13:15:53

标签: r rselenium

我的目标是从网址下载图片。在我的情况下,我无法使用download.file,因为我的图片位于需要登录的网页中,并且在真实图像可见之前,它会在后台运行一些java脚本。这就是我需要使用RSelenium包来实现的原因。

根据建议here,我已经构建了一个带有独立chrome标记的docker容器。 Docker终端的输出:

$ docker-machine ip
192.168.99.100
$ docker ps
CONTAINER ID  IMAGE                              COMMAND                CREATED             STATUS              PORTS                    NAMES
c651dab3a948  selenium/standalone-chrome:3.4.0  "/opt/bin/entry_po..."  24 hours ago        Up 24 hours         0.0.0.0:4445->4444/tcp   cranky_kalam

以下是我尝试的内容:

require(RSelenium)

# Avoid download prompt to pop up and parsing default download folder
eCaps <- list(
  chromeOptions = 
    list(prefs = list(
      "profile.default_content_settings.popups" = 0L,
      "download.prompt_for_download" = FALSE,
      "download.default_directory" = "C:/temp/Pictures"
    )
    )
)

# Open connection
remDr <- remoteDriver(remoteServerAddr = "192.168.99.100",port = 4445L,browserName="chrome",extraCapabilities = eCaps)
remDr$open()

# Navigate to desired URL with picture
url <- "https://www.google.be/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
remDr$navigate(url)
remDr$screenshot(display = TRUE) # Everything looks fine here

# Move mouse to the page's center
webElem <- remDr$findElement(using = 'xpath',value = '/html/body')
remDr$mouseMoveToLocation(webElement = webElem)

# Right click and 
remDr$click(2)
remDr$screenshot(display = TRUE) # I don't see the right-click dialog!
# Try to move right-click dialog to 'Save as' or 'Save image as'
remDr$sendKeysToActiveElement(list(key = 'down_arrow',
                                   key = 'down_arrow',
                                   key = 'enter'))
### NOTHING HAPPENS

我试图使用key = 'down_arrow'的金额来玩,每次调查C:/temp/Pictures都没有保存任何内容。

请注意,这只是一个示例,我知道我可以使用download.file下载此图片。对于我的实际情况,我需要一个RSelenium的解决方案。

0 个答案:

没有答案