RSelenium: server signals port is already in use

后端 未结 5 800
北恋
北恋 2020-12-14 08:42

I\'m using the following code in RSelenium to open a browser. After I close the browser, or even close the handler by running remDr$close(), the port is still in use. I have

相关标签:
5条回答
  • 2020-12-14 08:47

    One way to avoid this problem is to use free_port() to find a free port (rather than specifying it manually)

    library(netstat)
    rsDriver(verbose = FALSE,port=free_port())
    
    0 讨论(0)
  • 2020-12-14 09:03

    The process is composed of two parts a server (the Selenium Server) and a client (the browser you initiate). The close method of the remoteDriver class closes the client (the browser). The server also needs to be stopped when you are finished.

    To stop the server when you are finished:

    library(RSelenium)
    rD <- rsDriver(verbose = FALSE,port=4444L)
    remDr <- rD$client
    remDr$close()
    

    Now either explicitly stop the server:

    rD$server$stop()
    

    or if the rD object is removed the server will be stopped upon garbage collection:

    library(RSelenium)
    rD <- rsDriver(verbose = FALSE,port=4444L)
    remDr <- rD$client
    remDr$close()
    rm(rD)
    gc()
    
    0 讨论(0)
  • 2020-12-14 09:06

    The command:

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

    will free all the ports.

    If you want to free a particular port, you can do this:

    #get the PID of the process you launched

    pid <- driver$server$process$get_pid()
    

    #pasting this PID in the following command (will kill all the child processes as well, closes the browser as well)

    system(paste0("Taskkill /F /T" ," /PID ", pid))
    
    0 讨论(0)
  • 2020-12-14 09:08

    I did not have issues until recently. What worked for me is to use the solution above and as per the solution in this thread to add a line to kill the Java instance(s) inside RStudio.

    remDr$close()
    driver$server$stop()
    rm(driver, remDr)
    gc()
    
    system("taskkill /im java.exe /f", intern=FALSE, ignore.stdout=FALSE)
    
    0 讨论(0)
  • 2020-12-14 09:10

    What worked for me is not calling stop at all and only calling close.

    rD <- rsDriver(port = 4444L)
    remDr <- rD[["client"]]
    remDr$close()
    rm(rD)
    gc()
    

    EDIT: Nevermind - this worked last week several times and then hasn't worked again.

    0 讨论(0)
提交回复
热议问题