Select 下拉框中的选项使用 Rselenium
Select option in dropdown box using Rselenium
我正在尝试使用 RSelenium select https://analytics.phe.gov.uk/apps/covid-19-indirect-effects/# 心理健康和幸福标签下拉框中的主题 'loneliness'。我可以让 Rselenium 进入心理健康选项卡,但我在 selecting 'loneliness' 主题方面没有任何运气。我将不胜感激任何引导,因为我已经回顾了 Stack Overflow 中的许多帖子(你可以对我多次失败的尝试轻笑)但仍然没有快乐。
非常感谢任何指点!
library(tidyverse)
library(rvest)
library(RSelenium)
# Start Selenium server and browser
driver <- rsDriver(
browser = "chrome",
chromever = "97.0.4692.71"
)
remDr <- driver$client
# Navigate to wich tool
remDr$navigate("https://analytics.phe.gov.uk/apps/covid-19-indirect-effects/")
#changing the page to the mental health and well being tab
##avoided using href as the tab value keeps changing
webElem <- remDr$findElement(using = 'xpath', value = '//*[@id="page"]/nav/div/ul/li[8]/a')
# Highlight to check that was correctly selected - don't need but putting it in as a check
webElem$highlightElement()
# click the search link
webElem$clickElement()
# click the link
webElem$clickElement()
#multiple failed attempts of trying to selecting the theme = Loneliness - can't get to work :-(
##attempt 1
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()
webElem1 <- remDr$findElement(using = 'xpath', value = "//*[@data-selectable data-value='Loneliness']")
webElem1$highlightElement()
webElem1$clickElement()
##
##attempt 2
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()
webElem2 <- remDr$findElement(using = 'xpath', value = "/html/body/div[1]/div[2]/div/div[8]/div[3]/div[1]/form/div[1]/div/div/div[2]/div/div[2]")
webElem2$highlightElement()
webElem2$clickElement()
##
##attempt 3
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()
webElem3 <- remDr$findElement(using = 'xpath', value = "//*[text()='Loneliness']")
webElem3$highlightElement()
webElem3$clickElement()
##
##attempt 4
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()
webElem4 <- remDr$findElement(using = 'xpath', "/html/body/div[1]/div[2]/div/div[8]/div[3]/div[1]/form/div[1]/div/div/div[2]/div/[@value = 'Loneliness']")
webElem4$highlightElement()
webElem4$clickElement()
##
##attempt 5
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem[[2]]$clickElement()
看起来下拉菜单正在使用 selectize.js。像下面这样的东西似乎有效:
library(tidyverse)
library(rvest)
library(RSelenium)
# Start Selenium server and browser
driver <- rsDriver(
browser = "chrome",
chromever = "98.0.4758.80"
)
remDr <- driver$client
# Navigate to wich tool
remDr$navigate("https://analytics.phe.gov.uk/apps/covid-19-indirect-effects/")
select_tab <- remDr$findElement(using = "xpath", value = '//*[@id="indicatortable"]/div/div[1]/div[2]/div[6]/div/div[6]/div/a')
# Highlight to check that was correctly selected - don't need but putting it in as a check
select_tab$highlightElement()
select_tab$clickElement()
remDr$executeScript("
var s = $('#themeOPN').selectize()[0];
s.selectize.setValue('Loneliness');
")
我正在尝试使用 RSelenium select https://analytics.phe.gov.uk/apps/covid-19-indirect-effects/# 心理健康和幸福标签下拉框中的主题 'loneliness'。我可以让 Rselenium 进入心理健康选项卡,但我在 selecting 'loneliness' 主题方面没有任何运气。我将不胜感激任何引导,因为我已经回顾了 Stack Overflow 中的许多帖子(你可以对我多次失败的尝试轻笑)但仍然没有快乐。
非常感谢任何指点!
library(tidyverse)
library(rvest)
library(RSelenium)
# Start Selenium server and browser
driver <- rsDriver(
browser = "chrome",
chromever = "97.0.4692.71"
)
remDr <- driver$client
# Navigate to wich tool
remDr$navigate("https://analytics.phe.gov.uk/apps/covid-19-indirect-effects/")
#changing the page to the mental health and well being tab
##avoided using href as the tab value keeps changing
webElem <- remDr$findElement(using = 'xpath', value = '//*[@id="page"]/nav/div/ul/li[8]/a')
# Highlight to check that was correctly selected - don't need but putting it in as a check
webElem$highlightElement()
# click the search link
webElem$clickElement()
# click the link
webElem$clickElement()
#multiple failed attempts of trying to selecting the theme = Loneliness - can't get to work :-(
##attempt 1
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()
webElem1 <- remDr$findElement(using = 'xpath', value = "//*[@data-selectable data-value='Loneliness']")
webElem1$highlightElement()
webElem1$clickElement()
##
##attempt 2
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()
webElem2 <- remDr$findElement(using = 'xpath', value = "/html/body/div[1]/div[2]/div/div[8]/div[3]/div[1]/form/div[1]/div/div/div[2]/div/div[2]")
webElem2$highlightElement()
webElem2$clickElement()
##
##attempt 3
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()
webElem3 <- remDr$findElement(using = 'xpath', value = "//*[text()='Loneliness']")
webElem3$highlightElement()
webElem3$clickElement()
##
##attempt 4
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem$highlightElement()
webElem$clickElement()
webElem4 <- remDr$findElement(using = 'xpath', "/html/body/div[1]/div[2]/div/div[8]/div[3]/div[1]/form/div[1]/div/div/div[2]/div/[@value = 'Loneliness']")
webElem4$highlightElement()
webElem4$clickElement()
##
##attempt 5
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-shinyjs-resettable-id='themeOPN']")
webElem[[2]]$clickElement()
看起来下拉菜单正在使用 selectize.js。像下面这样的东西似乎有效:
library(tidyverse)
library(rvest)
library(RSelenium)
# Start Selenium server and browser
driver <- rsDriver(
browser = "chrome",
chromever = "98.0.4758.80"
)
remDr <- driver$client
# Navigate to wich tool
remDr$navigate("https://analytics.phe.gov.uk/apps/covid-19-indirect-effects/")
select_tab <- remDr$findElement(using = "xpath", value = '//*[@id="indicatortable"]/div/div[1]/div[2]/div[6]/div/div[6]/div/a')
# Highlight to check that was correctly selected - don't need but putting it in as a check
select_tab$highlightElement()
select_tab$clickElement()
remDr$executeScript("
var s = $('#themeOPN').selectize()[0];
s.selectize.setValue('Loneliness');
")