How to reopen selenium browser in a loop: Python

Viewed 721

I'm trying to close the browser and reopen it after a loop

class Bot():

driver = webdriver.Chrome(options=chrome_options)

    def __init__(self):
        self.openyoutube
        self.quitbrowser()

    def openyoutube(self):
        self.driver.get('https://www.youtube.com')
        sleep(5)
   
    def quitbrowser(self):
        self.driver.quit()



def main():
    while True: 
        my_bot = Bot()
        sleep(15)

if __name__ == '__main__':
    main()

But once it tries to start all over, it throws this error:

raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=64578): Max retries exceeded with url: /session/f7fcdfe14c3e2c75d530b3cbf70348d2/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb6d71f8250>: Failed to establish a new connection: [Errno 61] Connection refused'))

1 Answers
raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=64578): Max retries exceeded with url: /session/f7fcdfe14c3e2c75d530b3cbf70348d2/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb6d71f8250>: Failed to establish a new connection: [Errno 61] Connection refused'))

Generally after this error we reinitialise the driver which you can do by using a try catch for Maxretryerror.

driver = webdriver.Chrome(options=chrome_options)
Related