简体   繁体   English

在 RSelenium 中使用下拉值抓取表

[英]Scraping table with drop-down values in RSelenium

New to Selenium.硒的新手。 I'm trying to scrape results from this table populated with dynamic data:我试图从这个填充了动态数据的表中抓取结果:

奇怪的桌子

The Elements are as follows:元素如下: 要素

I wish I could add the link but it's password-protected and sharing would be a security issue.我希望我可以添加链接,但它受密码保护,共享将是一个安全问题。 I'm having trouble finding something comparable to post as a suitable example and suggestions are welcomed.我无法找到与 post 类似的内容作为合适的示例,欢迎提出建议。

For a previous table (also based on dynamic information, but not drop-downs) this code worked for me:对于以前的表(也基于动态信息,但不是下拉列表),此代码对我有用:

# WORKED FOR NON-DROPDOWN DYNAMIC TABLE
require(RSelenium)
pJS <- phantom()
Sys.sleep(5)
remDr <- remoteDriver(browserName = "phantomjs")
remDr$open()
url <- paste0("http://",un,":",pass,"@egauge", meter, ".egaug.es/57A4C/check.html")
remDr$navigate(url)
#remDr$screenshot(display = TRUE)
doc <- htmlParse(remDr$getPageSource()[[1]])
remDr$close()

readHTMLTable(doc)[[2]]

but if I use the same approach with the current table, it gives me back all the values in the drop-down box.但是如果我对当前表使用相同的方法,它会返回下拉框中的所有值。 My current attempt looks like this:我目前的尝试是这样的:

# NON-WORKING ATTEMPT FOR DROPDOWN DYNAMIC TABLE
require(RSelenium)

pJS <- phantom()
Sys.sleep(5)
remDr <- remoteDriver(browserName = "phantomjs")
remDr$open()
url <- paste0("http://",un,":",pass,"@egauge", meter, ".egaug.es/57A4C/settings.html")
remDr$navigate(url)
remDr$findElement("xpath", '//*[@id="regsDiv"]/div/table/tbody/tr[1]/td[3]/span[2]/select[1]')$selectTag()

But this only returns a list of Elements, options, and text.但这仅返回元素、选项和文本的列表。 I need the selected option.我需要选择的选项。 Once this works I loop through with tr[] (from the xpath) and scrape all the selected values.完成后,我使用tr[] (来自 xpath)循环并抓取所有选定的值。

Thanks!谢谢! Any help appreciated!任何帮助表示赞赏! I'm sorry I don't have a great working example.对不起,我没有一个很好的工作示例。

EDIT: updated for response.编辑:更新响应。

You can use the isElementSelected method:您可以使用isElementSelected方法:

remDr$navigate("http://nhb.gov.in/OnlineClient/MonthlyPriceAndArrivalReport.aspx")
elem <- remDr$findElement(
  using = "id", 
  value = "ctl00_ContentPlaceHolder1_ddlyear"
)
options <- elem$selectTag()
res <- vapply(options$elements, function(x) x$isElementSelected()[[1]], 
              logical(1))
res
> res
[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[16] FALSE FALSE FALSE

UPDATE: The dev branch now adds a selected field to the selectTag method:更新:dev 分支现在向selectTag方法添加了一个选定的字段:

devtools::install_github("ropensci/RSelenium")

remDr$navigate("http://nhb.gov.in/OnlineClient/MonthlyPriceAndArrivalReport.aspx")
elem <- remDr$findElement(
  using = "id", 
  value = "ctl00_ContentPlaceHolder1_LsboxCenterList"
)
options <- elem$selectTag()
options$selected
> options$selected
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[16] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[31] FALSE FALSE FALSE FALSE

# Now select "AGRA" and BANGALORE
myScript <- 
  "for (var i=0, iLen=arguments.length; i<iLen; i++) {
        arguments[i].selected = true;
        }"
remDr$executeScript(myScript, options$elements[c(2,5)])
newoptions <- elem$selectTag()
newoptions$selected
> newoptions$selected
[1] FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[16] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[31] FALSE FALSE FALSE FALSE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM