Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 97869

Selenium doesn't quit on KeyboardInterrupt (Python)

$
0
0

I'm trying to close Firefox instance that my app has opened when user interrupts the program. But selenium doesn't close the window. Here is a sample code that shows the problem:

import time

from selenium import webdriver

driver = webdriver.Firefox()

try:
    while True:
        driver.get("https://google.com")
        time.sleep(5)
except KeyboardInterrupt:
    print("Quitting")
    driver.quit()
    print("Done")

When I hit Ctrl+C, I see "Quitting" printed on the console, a few moments later, I see "Done" printed to the console and program ends. But firefox window remains open. How can I solve this issue?

Edit:

import time

from selenium import webdriver

driver = webdriver.Firefox()

try:
    for i in range(5):
        print("Looping")
        driver.get("https://google.com")
        time.sleep(5)
except KeyboardInterrupt:
    print("Quitting")
    driver.quit()
    print("Done")
driver.quit()
print("After loop and stuff")

This won't work neither. If you wait the loop out, browser closes successfully. But if you hit Ctrl+C in the middle of the loop, browser remains open.


Viewing all articles
Browse latest Browse all 97869

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>