Launch RSelenium Browser regardless of port open/closed












2















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!










share|improve this question























  • What operating system are you on?

    – hrbrmstr
    Nov 25 '18 at 11:20











  • @hrbmstr I use both mac and windows, but primarily windows for RSelenium.

    – Neal Barsch
    Nov 29 '18 at 3:59











  • sapply(4444:4450, function(p) setNames(list(pingr::ping_port("localhost", p)), p)) is a bit of toy code to see if there's something listening on a local port (all NA == very likely nothing there). It's cross platform and all in R (well, compiled C=>R) vs some system()-call based ones that could do something similar (hence my q abt OS)

    – hrbrmstr
    Nov 29 '18 at 13:37
















2















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!










share|improve this question























  • What operating system are you on?

    – hrbrmstr
    Nov 25 '18 at 11:20











  • @hrbmstr I use both mac and windows, but primarily windows for RSelenium.

    – Neal Barsch
    Nov 29 '18 at 3:59











  • sapply(4444:4450, function(p) setNames(list(pingr::ping_port("localhost", p)), p)) is a bit of toy code to see if there's something listening on a local port (all NA == very likely nothing there). It's cross platform and all in R (well, compiled C=>R) vs some system()-call based ones that could do something similar (hence my q abt OS)

    – hrbrmstr
    Nov 29 '18 at 13:37














2












2








2








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!










share|improve this question














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!







r selenium rselenium ropensci






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 4:25









Neal BarschNeal Barsch

635115




635115













  • What operating system are you on?

    – hrbrmstr
    Nov 25 '18 at 11:20











  • @hrbmstr I use both mac and windows, but primarily windows for RSelenium.

    – Neal Barsch
    Nov 29 '18 at 3:59











  • sapply(4444:4450, function(p) setNames(list(pingr::ping_port("localhost", p)), p)) is a bit of toy code to see if there's something listening on a local port (all NA == very likely nothing there). It's cross platform and all in R (well, compiled C=>R) vs some system()-call based ones that could do something similar (hence my q abt OS)

    – hrbrmstr
    Nov 29 '18 at 13:37



















  • What operating system are you on?

    – hrbrmstr
    Nov 25 '18 at 11:20











  • @hrbmstr I use both mac and windows, but primarily windows for RSelenium.

    – Neal Barsch
    Nov 29 '18 at 3:59











  • sapply(4444:4450, function(p) setNames(list(pingr::ping_port("localhost", p)), p)) is a bit of toy code to see if there's something listening on a local port (all NA == very likely nothing there). It's cross platform and all in R (well, compiled C=>R) vs some system()-call based ones that could do something similar (hence my q abt OS)

    – hrbrmstr
    Nov 29 '18 at 13:37

















What operating system are you on?

– hrbrmstr
Nov 25 '18 at 11:20





What operating system are you on?

– hrbrmstr
Nov 25 '18 at 11:20













@hrbmstr I use both mac and windows, but primarily windows for RSelenium.

– Neal Barsch
Nov 29 '18 at 3:59





@hrbmstr I use both mac and windows, but primarily windows for RSelenium.

– Neal Barsch
Nov 29 '18 at 3:59













sapply(4444:4450, function(p) setNames(list(pingr::ping_port("localhost", p)), p)) is a bit of toy code to see if there's something listening on a local port (all NA == very likely nothing there). It's cross platform and all in R (well, compiled C=>R) vs some system()-call based ones that could do something similar (hence my q abt OS)

– hrbrmstr
Nov 29 '18 at 13:37





sapply(4444:4450, function(p) setNames(list(pingr::ping_port("localhost", p)), p)) is a bit of toy code to see if there's something listening on a local port (all NA == very likely nothing there). It's cross platform and all in R (well, compiled C=>R) vs some system()-call based ones that could do something similar (hence my q abt OS)

– hrbrmstr
Nov 29 '18 at 13:37












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464640%2flaunch-rselenium-browser-regardless-of-port-open-closed%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464640%2flaunch-rselenium-browser-regardless-of-port-open-closed%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'