dropdown boxes in RSelenium
here is the code to select a drop down list based on xpath.
Since the dropdown is inside an iframe, I have to switch into that iframe first.
It probably is much easier in your situation.
New to RSelenium, check out the quick start tutorial, want to learn more about the function, refer to the pdf documentation.
require(RSelenium)
remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4444, browserName = "firefox")
remDr$open()
remDr$navigate("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select")
iframe <- remDr$findElement(using='id', value="iframeResult")
remDr$switchToFrame(iframe)
# change audi to whatever your option value is
option <- remDr$findElement(using = 'xpath', "//*/option[@value = 'audi']")
option$clickElement()
How to select dropdown box using Rselenium?
Using the following code I was able to get the browser to select the 2014/15 season. You will need to inspect the contents of the various drop-downs and expand this as required.
rD <- rsDriver(port=4444L,browser="chrome")
remDr <- rD$client
#navigate to main page
remDr$navigate('https://www.premierleague.com/stats/top/players/total_pass')
#find 'filter by season' box and click it
webElem <- remDr$findElement(using = 'xpath', value = "//*[@data-dropdown-block='FOOTBALL_COMPSEASON']")
webElem$clickElement()
#find 2014/15 season and click it
webElem1 <- remDr$findElement(using = 'xpath', value = "//*[@data-option-name='2014/15']")
webElem1$clickElement()
R - Rselenium - navigate drop down menu / list / box using = 'id'
With a little knowledge about XPath, adapting the linked solution which using XPath for your case should be straightforward, for example :
option <- remDr$findElement(using = 'xpath', "//select[@id='main_ddYear']/option[@value='2014']")
option$clickElement()
Brief explanation about the XPath :
//select[@id='main_ddYear']
: Find<select>
element, anywhere in the HTML, whereid
attribute value equals'main_ddYear'
/option[@value = '2014']
: From such<select>
element, return child<option>
wherevalue
attribute value equals'2014'
.
R: Select drop down option with RSelenium
I can not test the answer because I don't have the url, but usually, when I have to choose an element from a list, I do this:
You could create a xpath as in this way:
"//*/option[@value='Images']"
...
"//*/option[@value='Help']"
This could be work
remDr$findElement(using = 'xpath', value = "//*/option[@value='Images']")$clickElement()
but shouldn't it work, probably you have to click (activate) the box before to choose an element (as in this answer).
If you provide the url I could complete my answer and check it.
get the values from drop down menu using Rselenium
So you want to select the academic year,
library(RSelenium)
driver = rsDriver(port = 4741L, browser = c("firefox"))
remDr <- driver[["client"]]
remDr$navigate("https://assist.org/")
#Select the drop down menu on Academic Year
remDr$findElement(using = "xpath",'//*[@id="transfer-agreement-search"]/div[1]/ng-select/div') -> dropdownmenu
dropdownmenu$clickElement()
# Select the Academic year 2018-2019 (fullXpath)
remDr$findElement(using = "xpath",'/html/body/app-root/div[2]/app-home-component/section[2]/app-form-container/div/div[1]/app-transfer-agreements-form/div/div[2]/form/div[1]/ng-select/ng-dropdown-panel/div/div[2]/div[4]') -> acdyear
acdyear$clickElement()
Select option in dropdown box using Rselenium
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');
")
RSelenium: Select option from dropdown
As mentioned in one of the comments, the issue has not to do with RSelenium but with the docker used. I now use a chrome docker (standalone-chrome) that does not have the same issue with selecting an option in a dropdown.
Click on value in dropdown window using RSelenium
Firstly you have to click on input field:
input <- remDr$findElement(using = 'xpath', "//input[@class = 'select-dropdown']")
input$clickElement()
then options will be visible and you can select them using right xPath
:
option <- remDr$findElement(using = 'xpath', "//span[contains(., 'Tuzlanski (035)')]")
option$clickElement()
Probably you will may need to use this:
Sys.sleep(5) # wait 5 seconds
if the script will be too quick and will try to select dropdown element before dropdown will appear.
Summary code:
input <- remDr$findElement(using = 'xpath', "//input[@class = 'select-dropdown']")
input$clickElement()
Sys.sleep(5) # wait 5 seconds
option <- remDr$findElement(using = 'xpath', "//span[contains(., 'Tuzlanski (035)')]")
option$clickElement()
You have also wrong xPath
:
Related Topics
How to Get Parameters from Config File in R Script
Note in R Cran Check: No Repository Set, So Cyclic Dependency Check Skipped
How to Call External R Script from R Markdown (.Rmd) in Rstudio
How to Specify Command Line Parameters to R-Script in Rstudio
Switch R Script from Non-Interactive to Interactive
R: Reorder Facet_Wrapped X-Axis with Free_X in Ggplot2
The Result of Rpart Is Just with 1 Root
Creating a Continuous Heat Map in R
R 3.4.1 "Single Candle" Personal Library Path Error: Unable to Create 'Na'
R: Text Progress Bar in for Loop
Why Does Median Trip Up Data.Table (Integer Versus Double)
Excel Cell Coloring Using Xlsx
Ggplot Legend Issue W/ Geom_Point and Geom_Text
Merge/Combine Columns with Same Name But Incomplete Data