1

My IDE is eclipse and I'm using Selenium Webdriver on MAC. Every time I execute my code below, I want to kill/close the previously opened browser and then start a new instance and leave it on. As a result, only one instance of browser should be open at a time. I am not doing this for testing purpose. I'm just doing this because I want to automate a task.

This is the error I am getting everytime I execute my code:

Cannot run program "START": error=2, No such file or directory

This is my code:

public class Demo {

public static void main(String[] args){

    System.setProperty("webdriver.chrome.driver", "/Users/Downloads/chromedriver/chromedriver");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.google.com/");
    try{
        Runtime.getRuntime().exec("START taskkill /F /IM chrome.exe");
        System.exit(0);
    }
    catch(IOException io){
        System.out.println(io.getMessage());
    }

}

}

This is working fine on windows. How do I fix this on MAC/unix/ environment or what changes should I make in my code?

I was told this error is something to do with admin privileges so I should run eclipse as an admin. So I did this: sudo open Eclipse.app/ . So the eclipse opened as admin and executed the code but I was still getting the same error.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
jumbo_siopao
  • 157
  • 1
  • 9

1 Answers1

0

As per your question using the following line of code :

Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");

to kill the Browser Client process will be against all the Best Practices.

While automating through Selenium as per the best practices you should invoke the quit() method within the tearDown() {}. As per the WebDriver W3C Editor's Draft invoking quit() DELETEs the current browsing session through sending "quit" command with {"flags":["eForceQuit"]} and finally sends the GET request on /shutdown EndPoint.

As an example incase of GeckoDriver (W3C compliant WebDriver variant) here are the sequence of events :

1503397488598   webdriver::server   DEBUG   -> DELETE /session/8e457516-3335-4d3b-9140-53fb52aa8b74 
1503397488607   geckodriver::marionette TRACE   -> 37:[0,4,"quit",{"flags":["eForceQuit"]}]
1503397488821   webdriver::server   DEBUG   -> GET /shutdown

So on invoking quit() method, the Web Client session and the WebDriver instance gets killed completely. Hence you don't have to incorporate any additional steps which will be an overhead.

Solution

Replace the line of code :

Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");

With :

driver.quit();

Here you can find a detailed discussion on Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?

Additional Considerations

  • Ensure that the WebDriver Binary is having executable permission for non-root users. (chmod 777)
  • Ensure that the WebDriver Binary is present in the specified location.
  • Execute your Test as a non-root user.
  • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352