Question: Is there a function that will open a selenium browser if none is already open, or close the current browser and reset the port and relaunch a browser?
Rationale: I work with big loops in RSelenium that occasionally crash, so sometimes I don't know if a port is open or browser is open in looped code. I'd like a RSelenium launcher that launches a browser regardless if one is open or if the port is in use.
Attempt: I tried this approach with tryCatch() but I still get the wdman error that a port is open if I try to launch it twice:
browserpreference <- "chrome"
tryCatch({rD <- rsDriver(port = 4444L, browser = paste0(browserpreference))}
,error=function(rD,remDr){
try(remDr$close(), silent=T)
try(rD$server$stop(),silent=T)
try(suppressWarnings(rm(rD, envir = .GlobalEnv)), silent=T)
try(suppressWarnings(rm(remDr, envir = .GlobalEnv)), silent=T)
gc()
rD <- rsDriver(port = 4444L, browser = paste0(browserpreference))
})
If I try this twice I get this error:
Error in wdman::selenium(port = port, verbose = verbose, version = version, :
Selenium server signals port = 4444 is already in use.
Thanks!
I faced this issue, it is because of a java run time left behind for each orphaned port. Killing each of these frees up the port. use the following to terminate the java run time left behind :