Please help: JavascriptError using Selenium in RStudio-script

Hello,

I was running a script for webscraping in RStudio and got the following error:

Selenium message:javascript error: this.each is not a function
  (Session info: chrome=81.0.4044.129)
Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T21:30:10'
System info: host: 'xxxxxx', ip: 'xxx.xxx.x.xxx', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_231'
Driver info: driver.version: unknown

Error: 	 Summary: JavaScriptError
 	 Detail: An error occurred while executing user supplied JavaScript.
 	 class: org.openqa.selenium.JavascriptException
	 Further Details: run errorDetails method

I don't really understand what the problem is and how I might solve it. So just to be sure, I updated both R and RStudio and re-installed the packages I needed. I ran the script again, but I still get the same error message. Does anyone know how to solve this problem? Thank you in advance!

I am still quite new to this, so very concrete steps would be most practical for me. I shall include my full script below for reference. The error message occurs just before "#end of the main loop"

library(data.table)   # Required for rbindlist
library(dplyr)        # Required to use the pipes %>% and some table manipulation commands
library(magrittr)     # Required to use the pipes %>%
library(rvest)        # Required for read_html
library(RSelenium)    # Required for webscraping with javascript
library(lubridate)    # Required to collect dates
library(stringr)
library(purrr)


options(stringsAsFactors = F) #needed to prevent errors when merging data frames

#Paste the GoodReads Url
url <- "https://www.goodreads.com/book/show/1885.Pride_and_Prejudice?ac=1&from_search=true&qid=tIhtoSYMen&rank=1"

languageOnly = F #If FALSE, "all languages" is chosen

#Set your browser settings
rD <- rsDriver(port = 4445L, browser = "chrome", chromever = "81.0.4044.69")
remDr <- rD[["client"]]
remDr$setTimeout(type = "implicit", 2000)
remDr$navigate(url)

bookTitle = unlist(remDr$getTitle())
finalData = data.frame()

# Main loop going through the website pages
morePages = T
pageNumber =  1
while(morePages){
  
  #Select reviews in correct language. 
      #Look at the html code for the Goodreads page of the book in Google Chrome (right mouse klick, select "view page source"), NOT in Firefox. 
      #It will look like this: 
          #<select name="language_code" id="language_code">
          #<option value="">All languages</option><option value="de">Deutsch &lrm;(9)</option>
          #<option value="en">English &lrm;(25)</option><option value="es">Español &lrm;(1)</option>
  
      #Look at the sequence to deduct the numeral language code (the number included show the number of reviews, not the numeral language code)
      #In this example "all languages" is 1, "Deutsch" is 2, "English" is 3...
    
      #It should also work if you only fill in the numeral language code, and leave the other one empty.
  
  selectLanguage = if(languageOnly){
    selectLanguage = remDr$findElement("xpath", "//select[@id='language_code']/option[@value='']")
  } else {
    selectLanguage = remDr$findElement("xpath", "//select[@id='language_code']/option[1]")
  }
  
  selectLanguage$clickElement()
  Sys.sleep(3)
  
  #Expand all reviews
  expandMore <- remDr$findElements("link text", "...more")
  sapply(expandMore, function(x) x$clickElement())
  
  #Extracting the reviews from the page
  reviews <- remDr$findElements("css selector", "#bookReviews .stacked")
  reviews.html <- lapply(reviews, function(x){x$getElementAttribute("outerHTML")[[1]]})
  reviews.list <- lapply(reviews.html, function(x){read_html(x) %>% html_text()} )
  reviews.text <- unlist(reviews.list)
  
  #Some reviews have only rating and no text, so we process them separately
  onlyRating = unlist(map(1:length(reviews.text), function(i) str_detect(reviews.text[i], "^\\\n\\\n")))
  
  #Full reviews
  if(sum(!onlyRating) > 0){
    
    filterData = reviews.text[!onlyRating]
    fullReviews = purrr::map_df(seq(1, length(filterData), by=2), function(i){
      review = unlist(strsplit(filterData[i], "\n"))
      
      data.frame(
        date = mdy(review[2]), #date
        username = str_trim(review[5]), #user
        rating = str_trim(review[9]), #overall
        comment = str_trim(review[12]) #comment
      )
    })
    
    #Add review text to full reviews
    fullReviews$review = unlist(purrr::map(seq(2, length(filterData), by=2), function(i){
      str_trim(str_remove(filterData[i], "\\s*\\n\\s*\\(less\\)"))
    }))
    
  } else {
    fullReviews = data.frame()
  }
  
  
  #partial reviews (only rating)
  if(sum(onlyRating) > 0){
    
    filterData = reviews.text[onlyRating]
    partialReviews = purrr::map_df(1:length(filterData), function(i){
      review = unlist(strsplit(filterData[i], "\n"))
      
      data.frame(
        date = mdy(review[9]), #date
        username = str_trim(review[4]), #user
        rating = str_trim(review[8]), #overall
        comment = "",
        review = ""
      )
    })
    
  } else {
    partialReviews = data.frame()
  }
  
  finalData = rbind(finalData, fullReviews, partialReviews)
  
  #Go to next page if possible
  nextPage = remDr$findElements("xpath", "//a[@class='next_page']")
  if(length(nextPage) > 0){
    message(paste("PAGE", pageNumber, "Processed - Going to next"))
    nextPage[[1]]$clickElement()
    pageNumber = pageNumber + 1
    Sys.sleep(2)
  } else {
    message(paste("PAGE", pageNumber, "Processed - Last page"))
    morePages = FALSE
  }
  
}   
#end of the main loop

#Replace missing ratings by 'not rated'
finalData$rating = ifelse(finalData$rating == "", "not rated", finalData$rating)

#Stop server
rD[["server"]]$stop()

#set directory to where you wish the file to go
#copy your working directory and exchange all backward slashes with forward slashes
getwd()
setwd("")

#Write results
write.csv(finalData, paste0(bookTitle, ".csv"), row.names = F)
message("FINISHED!")

Edit: typo
Update: I also ran the commands "errorDetails method" and (when that didn't do anything) "errorDetails" in RStudio, but that only gave me Error: unexpected symbol.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

From the OP,

This issue was resolved after reinstalling java and installing rjava (https://cimentadaj.github.io/blog/2018-05-25-installing-rjava-on-windows-10/installing-rjava-on-windows-10/ )

1 Like