Rselenium:down_arrow在下拉菜单中不起作用

问题描述 投票:3回答:1

如果您能为我提供以下帮助,那就太好了:

我正在使用Rselenium和Firefox浏览以下网站:https://hcupnet.ahrq.gov/#setup

我无法使用down_arrow或使用window.scrollBy(0,1200)在下拉菜单上向下滚动而感到困惑。我的目标是从最初不可见的列表中进行诊断,然后选择它。

代码中提供了使用的完整x路径,但是如果您更喜欢通过浏览器浏览,则单击是:

  1. 创建新分析
  2. 社区
  3. 一年
  4. 亚利桑那
  5. 诊断步骤
  6. 诊断CSS
  7. 选择诊断下拉菜单

我在选择诊断下拉菜单中发现了问题。我的目标是选择一种诊断,例如“ 27卵巢癌”,该诊断位于列表中,起初不可见。

Website code

# Stackoverflow question
library(RSelenium)
library(wdman)
library(rvest)
library(tidyverse)

# Open docker -------------------------------------------------------------
remDr <- rsDriver(port = 4443L,
                  browser = "firefox")

remDr2 <- remDr[["client"]]

# Navigate landing website ------------------------------------------------

# Set window size
remDr2$setWindowSize(1280L, 1024L)

# Open landing website
remDr2$navigate("https://hcupnet.ahrq.gov/#setup") #Entering our URL gets the browser to navigate to the page
Sys.sleep(2)

# Function to explore xpaths ----------------------------------------------
click_xpath <- function(xpath){
  webElem <- remDr2$findElement(using = 'xpath', value = xpath )
  remDr2$mouseMoveToLocation(webElement = webElem)
  remDr2$click(1)
  Sys.sleep(2)
}


# Landing Page ------------------------------------------------------------

# Landing page paths
landing_xpaths <- c(
  '/html/body/div[1]/div/section[1]/div[3]/div[1]/button[1]', # 1. Create new analysis,
  '//*[@id="DS_COMM"]', # 2. Press community
  '//*[@id="YEAR_SINGLE"]', #3. Press single year,
  '/html/body/div[2]/div/div/div/div/div/div[2]/div[3]/section/div/div/button/span[1]', #4. Press state dropdown,
  '/html/body/div[6]/div/ul/li[2]/a', #5. Press Arizona
  '//*[@id="CL_COUNTY"]', #6. Press county
  '//*[@id="DP"]', # 7. Press diagnosis procedure
 '/html/body/div[2]/div/div/div/div/div/div[2]/div[6]/section/div/div/button/span[1]', #8 Press dropdown category
  '/html/body/div[6]/div/ul/li[2]/a', #9. Press diagnosis (CSS)
  '/html/body/div[2]/div/div/div/div/div/div[2]/div[7]/section/div/div/button/span[1]' ) #10. Press Diagnosis dropdown

map(landing_xpaths,click_xpath)

# Problem ------------------------------------------------------------

# I am not able to select diagnoses that are not visible, nor I am able to use the down_arrow to go down on the list.

###################################################
# Selecting: Not working
###################################################

click_xpath(" //span[@class='text'][contains(text(),'27 Cancer of ovary')]/parent::a ")

 Error:      Summary: MoveTargetOutOfBounds
     Detail: Target provided for a move action is out of bounds.
     class: org.openqa.selenium.interactions.MoveTargetOutOfBoundsException
     Further Details: run errorDetails method 


###################################################
# Scrolling down: Not working
###################################################
septicemia <- remDr2$findElement(using = 'xpath', value = '/html/body/div[6]/div/ul/li[1]/a')

# Notice that none works
septicemia$sendKeysToElement(list(key = "down_arrow"))
septicemia$sendKeysToElement(list(key = "down_arrow"))
septicemia$executeScript("window.scrollBy(0,1200)")


r selenium-webdriver rselenium
1个回答
2
投票

问题是,年份下拉列表并不完全是下拉列表。或至少不能像它那样与之交互。

这是来源的样子:Page Source如您所见,它是一系列div和list item标记,使其像下拉菜单一样[[look。

因此,如何选择一个选项是单击主要的“下拉”元素以显示选项列表

//button[@class='btn dropdown-toggle btn-default']

现在,我们单击所需选项的父元素(在这种情况下为a标记)。例如,如果我们要选择2012:

//span[@class='text'][contains(text(),'2012')]/parent::a


为响应您的更新,您有几个选项可以选择该下拉菜单中的值。

    使用现有的搜索框。
  • //div[@class='bs-searchbox']/input
    将所需的选择发送到该输入字段,然后发送回车键以选择显示的第一个值。请注意,您必须首先使下拉菜单可见。您也可以只单击现在可见的元素,因为搜索缩小了结果范围。

      使用executeScript单击不可见的元素。
  • webElem <- remDr2$findElement(using = 'xpath', value = "//span[contains(text(),'27 Cancer of ovary')]/parent::a") remDr2$executeScript("arguments[0].click();", list(webElem))
  • © www.soinside.com 2019 - 2024. All rights reserved.