how to close Python selenium webdriver window
Asked Answered
A

7

36

I have a python script that scraps data off from a website on an hourly basis. It is stored on the server at the moment and is working well as I am using task scheduler to schedule it to execute the script on an hourly basis.

I am using this code

driver.quit()

to quit the browser window

My problem to this that whenever I am not logging in to the server, it will start stacking up the webdriver window as somehow the driver.quit() function does not work when I am logging into the server. every morning when I came to work, I have tons of window to close from the server.

I tried to quit, close, dispose, but it doesn't help. What else I can try?

Asquint answered 4/3, 2017 at 1:49 Comment(3)
Does your code works with quit() when you execute manually with your console(terminal)?Inoculate
yes it does. quit doesnt work when i am not logging in to the serverAsquint
It honestly sounds like an exception is being raised before driver.quit() is executed. Are you sure your code is fully completing without any exceptions?Motorcar
K
48

In python using chromedriver, I quit Chrome processes with:

driver.close()
Kilimanjaro answered 27/1, 2018 at 2:15 Comment(0)
R
18

For Python and chromedriver I've found these two methods useful (mind the difference):

driver.close() - closes the browser active window.

driver.quit() - closes all browser windows and ends driver's session/process.

Rein answered 27/5, 2020 at 16:23 Comment(0)
T
3

For anyone still finding this question relevant in 2021 (heck it is for me!).

For me driver.quit() definitely was the right method. The main issue was tests not being cleaned up properly, meaning driver.quit() was never called.

  • Make sure you run cleanup steps regardless of if the test crashes. e.g. in unittest use the tearDown or tearDownClass methods (setup the driver in setUp.
  • If you can't do that wrap whatever you're doing with the driver object in a try / catch and do the driver.quit() in the finally block.
Teniacide answered 26/10, 2021 at 19:30 Comment(0)
G
2

Could'nt you scrape in headless mode? (example for chrome)

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.headless = True
...
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.create_options()
Gratulant answered 2/6, 2020 at 22:33 Comment(2)
Yes, but you still need to close. I'm actually here because I just realized this - I have scripts running headless firefox and the headless processes stay open, innocently loitering at first and eventually tanking your system under the oppressive weight of their legion.Schuman
Yes, true, but difficult to see what's wrong without his code.Gratulant
P
0

I use linux command to close all. Here're two commands I ran after all scraping job is done:

  • pkill firefox
  • pkill Xcfb
Putumayo answered 5/9, 2017 at 17:11 Comment(0)
D
0

You can close browser by calling JavaScript close methods like so.

driver.execute_script("window.close();")
Denyse answered 21/12, 2023 at 5:30 Comment(0)
E
-4

I think webDriver.Dispose() should work, It closes all browser windows. Here is a SO post about the 3 different ways to close a webdriver.

Eventide answered 4/3, 2017 at 4:46 Comment(1)
In python, all you get is AttributeError: 'WebDriver' object has no attribute 'Dispose'Battled

© 2022 - 2024 — McMap. All rights reserved.