How to Select Dropdown Box Using Rselenium

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()

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()

Sample Image

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.

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.

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

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:
Sample Image

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, where id attribute value equals 'main_ddYear'
  • /option[@value = '2014'] : From such <select> element, return child <option> where value attribute value equals '2014'.

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()

How to select item in dropdown list using Selenium?

I was able to get it work, although its a bit of roundabout way. I used pynput and some keyboard inputs to scroll through the dropdown and then select the account I wanted to pay from. Its not perfect but its a start and it works. Here's the final code I used:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from pynput.keyboard import Key, Controller

import yaml
import time

conf = yaml.load(open(r'D:\Users\Matt\Documents\GitHub\YML_Files\REI_Login_Credentials.yml'))
myREIUsername = conf['REILogin']['username']
myREIPassword = conf['REILogin']['password']

driver = webdriver.Firefox(
executable_path=
r'D:\Users\Matt\Documents\GitHub\Executable_Files\geckodriver.exe'
)

def login():
driver.get('https://onlinebanking.usbank.com/Auth/Login?usertype=REIMC&redirect=login&lang=en&exp=')
time.sleep(4)
driver.find_element_by_id('aw-personal-id').send_keys(myREIUsername)
driver.find_element_by_id('aw-password').send_keys(myREIPassword)
time.sleep(2)
driver.find_element_by_id('aw-log-in').click()
time.sleep(10)
make_payment()

def make_payment():
if (driver.find_element_by_class_name('accountRowLast').text) != "0.00":
driver.find_element_by_css_selector('a.soloLink.accountNamesize').click()
time.sleep(3)
driver.find_element_by_xpath('/html/body/div[4]/div[2]/div[2]/div[2]/div[4]/div[3]/div[10]/div[2]/div[2]/a').click()
time.sleep(5)
accounts = driver.find_elements_by_class_name('ui-selectmenu-status')
accounts[1].click()
keyboard = Controller()
keyboard.press(Key.down)
time.sleep(1)
keyboard.release(Key.down)
keyboard.press(Key.enter)
keyboard.release(Key.enter)
driver.find_element_by_id('rdoAmountOptionSingleCurrent_1').click()
driver.find_element_by_id('btnTPContinue').click()
time.sleep(1)
driver.find_element_by_id('btnSubmit').click()
driver.quit()
else:
driver.quit()

login()


Related Topics



Leave a reply



Submit