按RSelenium中的提交按钮

时间:2016-12-12 16:52:50

标签: r selenium rselenium

我试图点击此处的Go按钮: http://ideal-scope.com/online-holloway-cut-adviser/ 我已经开始使用RSelenium ......

library(RSelenium)
RSelenium::startServer()
pJS <- phantom()
Sys.sleep(5) # give the binary a moment
remDr <- remoteDriver(browserName = 'phantomjs')
remDr$open()

...访问了封闭的iFrame ......

remDr$navigate("http://ideal-scope.com/online-holloway-cut-adviser/")
Sys.sleep(5)
webElems <- remDr$findElements("css", "iframe")
remDr$switchToFrame(webElems[[1]])

...并解压缩按钮。

 subElem   <- remDr$findElement("xpath", '//input[@type="submit"]')

但是当我尝试

subElem$sendKeysToElement(list("\uE007"))
subElem$sendKeysToElement(list(key = "space"))

什么都没发生。 我知道我可以访问该页面,因为我已成功更改了这些输入框中的值。例如:

depthElem <- remDr$findElement("name","depth_textbox")
depthElem$clearElement()
depthElem$sendKeysToElement(list(diamondsDT[theRow]$DepthPct))

现在我正在检查http://localhost:4444/wd/hub/static/resource/hub.html以查看网页。在尝试按下之后,没有任何变化,但我仍然可以在框中看到我的更新值。

1 个答案:

答案 0 :(得分:1)

如果不使用RSelenium,您可以直接使用 iframe POST表单。您只需按如下方式设置referer标头:

require(httr)
require(rvest)

q <- list(
  depth_textbox = 60,
  table_textbox = 57,
  crown_listbox = 0,
  crown_textbox = 34,
  pavilion_listbox = 0,
  pavilion_textbox = 40.5,
  cutlet_textbox = 0
)

my_url <- "http://www.pricescope.com/hca.php" # url of the iframe:
doc <- POST(my_url,
          body = q, encode = "form",
          # THIS IS THE CRUTIAL LINE
          add_headers(Referer = "http://ideal-scope.com/online-holloway-cut-adviser/")) %>% 
  read_html

现在,在您提交表单后,doc就是 iframe - 内容。 如果你想提取img并绘制它,你可以这样做:

img_url <- doc %>% html_nodes("img") %>% html_attr("src") %>% .[[2]]
tmp_file <- tempfile()
GET(xml2::url_absolute(img_url, my_url), write_disk(tmp_file))
#install.packages("ReadImages")
library('ReadImages')
plot(1:2, type="n")
rasterImage(readJPEG(tmp_file), 1, 1, 2, 2)

结果是:

enter image description here

相关问题