使用 RSelenium 读取下拉菜单元素中的值 [英] Read values in dropdown menu element with RSelenium

查看:20
本文介绍了使用 RSelenium 读取下拉菜单元素中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RSelenium 导航到站点并与元素进行交互.

问题:使用 RSelenium,我如何读取下拉菜单中的选项列表,以便识别最新的可用月份并使用它来将下拉菜单设置为正确的值?>

到 7 月底,下拉菜单将有 6 个选项,从 1 月到 6 月.请注意,特定月份的数据会在该月结束后大约 4-6 周发布,因此您要到 7 月下旬才能获得 6 月的数据,依此类推.我可以使用数字 1 到 12 设置值而不会出现任何问题,前提是下拉列表实际上包含相应的选项.

例如,如果 7 月的数据尚未发布(即,如果 7 月尚未出现在选项下拉列表中),并且如果我尝试将下拉列表设置为7",我将得到一个错误,我想避免.数据不会定期出现,所以我不能简单地假设 X 月的数据会出现在 Y 日.

我相信 Selenium 本身有 a辅助类可用,但我不确定是否可以通过 RSelenium 访问它.

示例代码如下 - 可能有点麻烦,因为我对这个包还很不熟悉.

库(RSelenium)RSelenium::checkForServer()RSelenium::startServer()remDr <- remoteDriver(remoteServerAddr = "localhost",端口 = 4444,浏览器名称 = "火狐")remDr <- remoteDriver()系统睡眠(5)remDr$open()remDr$getStatus()remDr$navigate("http://jamaserv.jama.or.jp/newdb/eng/index.html")##切换到左框架frameElems <- remDr$findElements(using = "tag name", "frame")sapply(frameElems, function(x){x$getElementAttribute("src")})remDr$switchToFrame(frameElems[[1]])webElem <- remDr$findElement(using = 'xpath', "//select[@name='txtTargetToMm']")##使用javascript设置值script <- paste0("arguments[0].value = '", 2, "'; arguments[0].onchange();") # 设置为二月remDr$executeScript(脚本,列表(webElem))webElem$getElementAttribute('value') # 检查它是否有效

解决方案

您可以使用 selectTag 方法与 HTML select 元素进行交互:

库(RSelenium)rD <- rsDriver(verbose = F)remDr <- rD$clientremDr$navigate("http://jamaserv.jama.or.jp/newdb/eng/index.html")##切换到左框架frameElems <- remDr$findElements(using = "tag name", "frame")sapply(frameElems, function(x){x$getElementAttribute("src")})remDr$switchToFrame(frameElems[[1]])webElem <- remDr$findElement(using = 'xpath', "//select[@name='txtTargetToMm']")opts <- webElem$selectTag()>选择$文本[1]一月"二月"三月"四月"五月"六月"七月"八月"[9]九月"十月"十一月"十二月">选项$值[1]1"2"3"4"5"6"7"8"9"10"11"12">选择$选择[1] 假假假假假假假假假假真假# 选择第三个月(三月)opts$elements[[3]]$clickElement()# 检查是否被选中opts <- webElem$selectTag()选择$选择>选择$选择[1] 假假真假假假假假假假假假rm(rD)GC()

I am using RSelenium to navigate to sites and interact with the elements.

Question: using RSelenium, how can I read the list of options in a dropdown menu so that I can identify the latest month available and use that to set the dropdown to the correct value?

On a certain site a dropdown menu is provided for the user to set the month of the year, thus defining the end point of a date range used in turn to display or download monthly data. As additional months of data become available through the year, the options in the dropdown change.

By the end of July the dropdown will have 6 options, January to June. Note that the data for a particular month is released about 4-6 weeks after the end of that month, so you do not get June's data until late July, and so on. I can set the values using numbers 1 to 12 without any problems, provided that the dropdown actually contains the corresponding option.

If, for example, the data for July has not yet been published (that is, if July has not yet appeared in the dropdown list of options) and if I try to set the dropdown to '7' I will get an error, which I want to avoid. The data does not appear on a regular schedule, so I cannot simply assume that the data for month X will appear on date Y.

I believe that with Selenium itself there is a helper class available, but I'm not sure whether I can access that through RSelenium.

Example code follows - may be a bit ropey as I'm still far from familiar with the package.

library(RSelenium)
RSelenium::checkForServer()
RSelenium::startServer()

remDr <- remoteDriver(remoteServerAddr = "localhost",
                      port = 4444,
                      browserName = "firefox"
                      )
remDr <- remoteDriver()
Sys.sleep(5)
remDr$open()
remDr$getStatus()

remDr$navigate("http://jamaserv.jama.or.jp/newdb/eng/index.html")

## Switch to left frame
frameElems <- remDr$findElements(using = "tag name", "frame")
sapply(frameElems, function(x){x$getElementAttribute("src")})
remDr$switchToFrame(frameElems[[1]])

webElem <- remDr$findElement(using = 'xpath', "//select[@name='txtTargetToMm']")

## Use javascript to set values
script <- paste0("arguments[0].value = '", 2, "'; arguments[0].onchange();") # set to February
remDr$executeScript(script, list(webElem))
webElem$getElementAttribute('value')    # check to see if it worked

解决方案

You can use the selectTag method to interact with HTML select elements:

library(RSelenium)

rD <- rsDriver(verbose = F)
remDr <- rD$client
remDr$navigate("http://jamaserv.jama.or.jp/newdb/eng/index.html")

## Switch to left frame
frameElems <- remDr$findElements(using = "tag name", "frame")
sapply(frameElems, function(x){x$getElementAttribute("src")})
remDr$switchToFrame(frameElems[[1]])

webElem <- remDr$findElement(using = 'xpath', "//select[@name='txtTargetToMm']")
opts <- webElem$selectTag()

> opts$text
[1] "JANUARY"   "FEBRUARY"  "MARCH"     "APRIL"     "MAY"       "JUNE"      "JULY"      "AUGUST"   
[9] "SEPTEMBER" "OCTOBER"   "NOVEMBER"  "DECEMBER" 
> opts$value
[1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
> opts$selected
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE

# select the third month (march)
opts$elements[[3]]$clickElement()

# check whether it is selected
opts <- webElem$selectTag()
opts$selected

> opts$selected
[1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

rm(rD)
gc()

这篇关于使用 RSelenium 读取下拉菜单元素中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆