How to avoid selenium driver closing automatically?

Issue

Before my question, it might be helpful to show you the general structure of my code:

class ItsyBitsy(object):
    def __init__(self):
        self.targets_a = dict() # data like url:document_summary 
                                # filled by another function  


    def visit_targets_a(self):
        browser = webdriver.Safari()

        for url in self.targets_a.keys():
            try:
                browser.switch_to.new_window('tab')
                browser.get(url)
                time.sleep(2)

            except Exception as e:
                print(f'{url} FAILED: {e}')
                continue

            # do some automation stuff

            time.sleep(2)

        print('All done!')

I can then instantiate the class and call my method without any issues:

spider = ItsyBitsy()
spider.visit_targets_a()


>>> All done!

However after every tab is opened and automations are completed, the window closes without any prompt even though I do not have browser.close() or browser.exit() anywhere in my code.

My band-aid fix is calling time.sleep(9999999999999999) on the last loop, which keeps the window open indefinitely due to the Overflow Error, but it is obviously not a solution.

So, how do I stop the browser from exiting?!

Bonus points if you can educate me on why this is happening.

Thanks guys/gals!

Solution

You need to override exit and prevent ‘browser.quit()’ from automatically happening.

This keeps the browser open if you set teardown=False:

class ItsyBitsy(object):
    def __init__(self, teardown=False):
        self.teardown = teardown
        self.targets_a = dict() # data like url:document_summary 
                                # filled by another function
        self.browser = webdriver.Safari()


    def visit_targets_a(self):

        for url in self.targets_a.keys():
            try:
                self.browser.switch_to.new_window('tab')
                self.browser.get(url)
                time.sleep(2)

            except Exception as e:
                print(f'{url} FAILED: {e}')
                continue

            # do some automation stuff

            time.sleep(2)

        print('All done!')

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.teardown:
            self.browser.quit()

spider = ItsyBitsy(teardown=False)
spider.visit_targets_a()

Answered By – BillyZee

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published