stevec
stevec

Reputation: 52268

Correct way to completely end an RSelenium session?

What is the 'correct' way to completely end an RSelenium session (including all of its constituent parts) from R?

Background

When using RSelenium for browser automation, there are many technologies interacting, and it can sometimes create weird bugs where everything in the R session is cleaned up, but some underlying chrome / chromedriver / phantom.js / selenium / (other?) processes haven't ended. This can cause problems when future RSelenium sessions are attempted.

What I know so far

The RSelenium docs, show two ways to close some parts of the overall process:

Method 1

close the browser, and then stop the server:

# start a chrome browser
rD <- rsDriver()
remDr <- rD[["client"]]
remDr$navigate("http://www.google.com/ncr")
remDr$navigate("http://www.bbc.com")
remDr$close()

# stop the selenium server
rD[["server"]]$stop()

Method 2

Remove the rD (remote driver) object, and call for garbage collection:

# if user forgets to stop server it will be garbage collected.
rD <- rsDriver()
rm(rD)
gc(rD)

Another thing I discovered (from a similar question about python)is rD$client$quit()

Question

With this assortment of methods available, precisely what should be done (i.e. is best practice) to be completely sure that an RSelenium session (and every process connected to that session - e.g. chrome/chromedriver etc) has completely closed, so it cannot possibly interfere with other RSelenium sessions?

Upvotes: 5

Views: 1822

Answers (2)

luis vergara
luis vergara

Reputation: 141

I had to go even further to enable the next script to run without running into any error.

My solutions is to run the following commands at the end

remD$closeWindow()

rD1 <- rD1$server$stop()

remD$closeall()

system("taskkill /im java.exe /f", intern=FALSE, ignore.stdout=FALSE)
system("taskkill /F /IM ChromeDriver.exe", intern=FALSE, ignore.stdout=FALSE) 

Upvotes: 1

Wimpel
Wimpel

Reputation: 27732

I encoutered problems, where Rselenium would say (when starting a new session) a port is still in use, even after closing everything down as mentioned in the question.

I found out from here and here that you (at least on windows) also need to close the java session in Rstudio by:

system("taskkill /im java.exe /f", intern=FALSE, ignore.stdout=FALSE)

after closing the session and stopping the server and running gc()

Upvotes: 7

Related Questions