Selenium webdriver + PhantomJS processes not closing

Asked
Active3 hr before
Viewed126 times

6 Answers

seleniumprocessesphantomjs
90%

First of all, it is clear that code snippet above has sever memory/resource leaks — server and proxy are not closed,,When I’ve added the same logic to close all ancestor processes that was opened in Web Driver initialization, this never happens again

Example_snippet/controller/utility/_selenium.js/ path = ‘bin/browsermob-proxy’ . . .
path = ‘bin / browsermob - proxy’ #your path to browsermob - proxyserver = Server(path) server.start() proxy = server.create_proxy()
Step 2 continued with class Config(object): CHROM. . .
class Config(object): CHROME_PATH = '/Library/Application Support/Google/chromedriver76.0.3809.68'
BROWSERMOB_PATH = '/usr/local/bin/browsermob-proxy-2.1.4/bin/browsermob-proxy'
class Docker(Config): CHROME_PATH = '/usr/local/bin/chromedriver'
import Server from seleniumimport contextlibfrom settings
import Config as config @contextlib.contextmanager def browser_and_proxy(): server = Server(config.BROWSERMOB_PATH) server.start() proxy = server.create_proxy() #...# Set up Chrome option = webdriver.ChromeOptions() option.add_argument('--proxy-server=%s' % proxy.proxy) prefs = {
   "profile.managed_default_content_settings.images": 2
}
option.add_experimental_option("prefs", prefs) option.add_argument('--headless') option.add_argument('--no-sandbox') option.add_argument('--disable-gpu') capabilities = DesiredCapabilities.CHROME.copy() capabilities['acceptSslCerts'] = True capabilities['acceptInsecureCerts'] = True path = config.CHROME_PATH browser = webdriver.Chrome(options = option, desired_capabilities = capabilities, executable_path = path) try: yield browser, proxy
finally: browser.quit() server.stop() #...with browser_and_proxy() as(browser, proxy): browser.get('https://www.airbnb.com/s/Seattle--WA--United-States/homes') #...
load more v
88%

As of July 2016, driver,close() and driver

Example_snippet/controller/utility/_selenium.js/ from selenium import webdriver. . .
from selenium
import webdriver
driver = webdriver.PhantomJS()
driver.get(url)
html_doc = driver.page_source
driver.close()
load more v
72%

And as described we have a couple of ports in CLOSE_WAIT stemming from the test program,is no _phantomjs_ process running,All of ports are actually found in phantomjsdriver,log (cadkey-tablet is actually port 1400),If you simple run "driver

Example_snippet/controller/utility/_selenium.js/ application> ps -ef | grep pha. . .
application > ps - ef | grep phantom
501 1021 460 0 9: 26 PM ttys000 0: 00.71 java - jar openfile.jar / usr / local / Cellar / phantomjs / 2.1 .0 / bin / phantomjs
501 1073 1062 0 9: 26 PM ttys000 0: 00.08 / usr / local / Cellar / phantomjs / 2.1 .0 / bin / phantomjs--webdriver = 36418--webdriver - logfile = /Users/sgoeschl / work / github / cgamache / openfile / target / phantomjsdriver.log
load more v
65%

PhantomJS web driver stays in memory , The tags are not closed in your ,

Example_snippet/controller/utility/_processes.js/ Driver = new PhantomJSDriver(). . .
Driver = new PhantomJSDriver();
load more v
75%

Terminate the process with kill/taskkill: After each execution, kill the PhantomJS process at the OS level,,Kill all PhantomJS processes at app startup in case the previous termination of the app service was not graceful – mitigates the case where the service was killed right after a new PhantomJS was started but the PID wasn’t recorded yet,Use pkill for Linux and taskkill for Windows to terminate all PhantomJS PIDs all together before each execution,Immediately after execution kick off, take a record of all the PhantomJS PIDs

Example_snippet/controller/utility/_processes.js/ static class PhantomJSUtils { . . .
static class PhantomJSUtils {
   private static final String PKILL_PHANTOM = "pkill -f phantomjs";
   private static final String TASKKILL_PHANTOM = "taskkill /IM \"phantomjs.exe\" /f";
   private static final String OS_WINDOWS_ID = "win";

   public static void killPhantomJsProcesses() throws InterruptedException {
      String os = System.getProperty("os.name");

      try {
         if (os.toLowerCase().contains(OS_WINDOWS_ID)) {
            Process p = Runtime.getRuntime().exec(TASKKILL_PHANTOM);
            p.waitFor();
         } else {
            Process p = Runtime.getRuntime().exec(PKILL_PHANTOM);
            p.waitFor();
         }
      } catch (IOException e) {
         LOGGER.error("Could not successfully send kill command to stop PhantomJS", e);
      }
   }
}
40%

and find 10 dangling phantomjs processes,,On PhantomJS 1

Example_snippet/controller/utility/_processes.js/ public static void main(String. . .
public static void main(String[] args) {
   int max = 10;
   for (int i = 0; i < max; i++) {
      WebDriver d1 = getDriver();

      d1.get("http://www.imdb.com/title/tt1951264");

      System.out.println("done with cycle " + (i + 1) + " of " + max);
      d1.quit();
   }

   System.out.println("done");
   System.exit(0);
}