0
import sys
link = sys.argv[1]
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'/usr/bin/geckodriver')
driver.get(link)
s = driver.page_source
print((s.encode("utf-8")))
driver.quit()

This is my code using python firefox and selenium, it works fine at the beginning in my server but then this script made some errors on my server because of lack memory. System admins say that my script does not close tabs, I put in my script this part:

driver.quit()

but it does seem it's not working, how should i close tabs then?

  • Possible duplicate of [Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?](https://stackoverflow.com/questions/47999568/selenium-how-to-stop-geckodriver-process-impacting-pc-memory-without-calling) – Dev Sep 02 '19 at 18:03

1 Answers1

1

First understand the difference between quit() and close() and if that does'nt solve your problem you may need below options to close the browser and drivers both from the root to free up the memory

For firefox browser

import subprocess
subprocess.call("taskkill /IM firefox.exe")

For geckodriver -

import subprocess
subprocess.call("taskkill /IM geckodriver.exe")

For chrome browser -

subprocess.call("TASKKILL /f  /IM  CHROME.EXE")

For chrome driver -

subprocess.call("TASKKILL /f  /IM  CHROMEDRIVER.EXE")

* Output *

>>> subprocess.call("taskkill /IM geckodriver.exe")  ## when there is no process running with name geckodriver it will return 128
128
>>> subprocess.call("TASKKILL /f  /IM  CHROME.EXE")  
128
>>> subprocess.call("TASKKILL /f  /IM  CHROMEDRIVER.EXE")  ## when there is running process of name chromedriver.exe it will return 0
0
>>> subprocess.call("taskkill /IM firefox.exe")
0

Just for info to do same in C#

Dev
  • 2,739
  • 2
  • 21
  • 34
  • in this link https://stackoverflow.com/questions/15067107/difference-between-webdriver-dispose-close-and-quit is explanation that quit should solve my problem, but it does not. –  Sep 02 '19 at 17:51
  • because there are some dangling instances other than what quit() method does and those can be killed by using command as mentioned in answer – Dev Sep 02 '19 at 18:02
  • aha but i didnt udnerstand how i shoud add this command into my code, i want to kill firefox and geckdriver, so i need two subbproces? –  Sep 02 '19 at 18:17
  • you must have some mechanism if you are running many testcases like `@aftermthod, @beforeMethod` you can call this into those methods, if you are executing only couple of testcases you can add this at the beginning and ending of each script – Dev Sep 02 '19 at 18:23
  • im calling this python script from php, so i dont know, where should i add this, in php or python? –  Sep 02 '19 at 18:32
  • obviously in python script – Dev Sep 02 '19 at 18:33
  • so my code should be like this https://repl.it/repls/FrankNaturalApplicationpackage –  Sep 02 '19 at 18:37
  • it would be better if you close existing browsers which are already open before executing your script on server and of course if you don't want then you can omit them, to do so add those lines before (line 6) options – Dev Sep 02 '19 at 18:45
  • why should i kill them before line6, i need them for opening page? –  Sep 02 '19 at 19:38
  • do you have already open browsers before running this script on server? – Dev Sep 02 '19 at 19:40
  • i kill them all on server –  Sep 02 '19 at 19:44
  • how did you killed them? – Dev Sep 02 '19 at 19:45
  • is this one time activity for you? – Dev Sep 02 '19 at 19:45
  • kill (number of process) –  Sep 03 '19 at 09:56