Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select option in dropdown box using Rselenium

I'm trying to use RSelenium to select the theme 'loneliness' from the drop-down box in the tab mental health and wellbeing from https://analytics.phe.gov.uk/apps/covid-19-indirect-effects/#. I can get Rselenium to go the the mental health tab but I haven't had any luck in selecting the 'loneliness' theme. I would be grateful for any steer as I've reviewed many posts from Stack Overflow (you can chuckle at my many failed attempts) and still no joy.

I would be really grateful for any pointers!

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()
like image 327
Dolphin Avatar asked Sep 20 '25 10:09

Dolphin


1 Answers

Looks like the dropdowns are using selectize.js. Something like the below seems to work:

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');
")

like image 126
Tom Jemmett Avatar answered Sep 22 '25 01:09

Tom Jemmett