phantomjs icon indicating copy to clipboard operation
phantomjs copied to clipboard

(JAVA selenium phantomjs web driver) get started phantomjs PID

Open wowhops opened this issue 10 years ago • 5 comments

Hello!

I'm using: -JAVA -Selenium -PhantomJS webdriver for Selenium.

Finishing a work with webdriver by browser.quit(); Sometimes it doesn't kill the spawned phantomjs process and if i run multiple phantomjs instances, after some time my memory is filled with zombie processes.

Question: How can i get a spawned phantomjs process's PID?

This is how i initialize phantomjs driver:

        ArrayList<String> cliArgsCap = new ArrayList<>();
        cliArgsCap.add("--ignore-ssl-errors=true");
        cliArgsCap.add("--ssl-protocol=any");
        cliArgsCap.add("--web-security=false");
        cliArgsCap.add("--load-images=false");

        final DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
        capabilities.setCapability(
                    PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                    System.getProperty("user.dir") + "\\phantomjs.exe"
            );

         WebDriver browser = new PhantomJSDriver(capabilities);

Note: want use to kill exactly the spawned process by PID, not all phantomjs processes.

similar topic: http://stackoverflow.com/questions/35069929/how-to-obtain-phantomjs-exe-pid-after-launch-in-java-selenium-non-os-method

wowhops avatar Feb 12 '16 10:02 wowhops

this is the most common error which is hanging a process:

[INFO  - 2016-02-12T11:16:58.526Z] ShutdownReqHand - _handle - About to shutdown
12-Feb-2016 18:17:01.034 INFO [Forwarding quit on session 08841f80-d17a-11e5-a528-99e475fb15df to remote] org.openqa.selenium.os.UnixProcess$SeleniumWatchDog.destroyHarder Command failed to close cleanly. Destroying forcefully (v2). org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@68c50d00
2016-02-12 18:17:12,040 ERROR [pool-14-thread-1] myClass.MainWork (MainWork.java:86) - Error closing the browser: Error communicating with the remote browser. It may have died.
Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39'
System info: host: 'slipu-PC', ip: '192.168.1.100', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_71'
Driver info: driver.version: RemoteWebDriver

wowhops avatar Feb 12 '16 11:02 wowhops

This is how i manage to deal with it, but sure it doesn't solving a prob itself

    public static void shutdownBrowser(WebDriver browser){
        for(int count =0; count < 10; count++) {
            int errorCheck =0;
            try {
                Thread.sleep(5000);
                browser.quit();
            } catch (Exception ex) {
                errorCheck = 1;
            }
            if(errorCheck == 0)
                break;
        }
    }

wowhops avatar Feb 13 '16 14:02 wowhops

The org.openqa.selenium.remote.service.DriverService has a method stop().

so in the PhantomJSDriver, i add code:

   public PhantomJSDriver(PhantomJSDriverService service, Capabilities desiredCapabilities) {
        super(new PhantomJSCommandExecutor(service), desiredCapabilities);
        this.driverService = service;
    }

    public PhantomJSDriverService getDriverService() {
        return driverService;
    }

Then when quit() do not work, do getDriverService().stop();

anexplore avatar Dec 06 '16 09:12 anexplore

@opahopa are you running phantomjs-prebuilt? if you are then you may be terminating the nodejs process not the phantomjs process. for me this seemed to happen a lot on nodejs 0.10.

jesg avatar Dec 06 '16 12:12 jesg

Due to our very limited maintenance capacity, we need to prioritize our development focus on other tasks. Therefore, this issue will be automatically closed (see #15395 for more details). In the future, if we see the need to attend to this issue again, then it will be reopened. Thank you for your contribution!

stale[bot] avatar Dec 29 '19 15:12 stale[bot]