简体   繁体   中英

How to run javascript using RSelenium?

I am trying to click an input object on a webpage using a JS call. First, I'm using RSelenium to pull up the page:

library(RSelenium)

rD <- rsDriver(port = 4444L, verbose = FALSE)
remDr <- rD$client
remDr$navigate('https://www.tripadvisor.com/Hotel_Review-g293913-d306432-Reviews-Ambassador_Hotel_Taipei-Taipei.html')

From the page I'm trying to click on the button "All languages" from the menu below:

在此处输入图片说明

I was trying to trigger the click with a call to JS, which works when running the script in the chrome console

script <- "document.getElementById('filters_detail_language_filterLang_ALL').click();"
remDr$executeScript(script, args=list())

But I get the following error:

remDr$executeScript(script, args = list())

Selenium message:unknown error: 'args' must be a list (Session info: chrome=64.0.3282.186) (Driver info: chromedriver=2.36.540469 (1881fd7f8641508feb5166b7cae561d87723cfa8),platform=Mac OS X 10.12.4 x86_64)

Error: Summary: UnknownError Detail: An unknown server-side error occurred while processing the command. Further Details: run errorDetails method

There may be a bug in RSelenium as such.

Try passing an dummy argument in the list method .

Use this:

remDr$executeScript(script, args = list("fugazi"))
element = remDr$findElement(using='xpath','//*
[@id="filters_detail_language_filterLang_ALL"]')
script <-"document.getElementById('filters_detail_language_filterLang_ALL').click();"
remDr$executeScript(script, args=list(element))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM