Python Forum
Selenium how to detect if browser is close? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Selenium how to detect if browser is close? (/thread-16370.html)



Selenium how to detect if browser is close? - gahhon - Feb-25-2019

CommonChrome.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
 
class ChromeDriver:
 
    def __init__(self, remote = False):
        driverOtp = webdriver.ChromeOptions()
        driverOtp.add_argument("--start-maximized")
        driverOtp.add_argument("--disable-infobars")
        driverOtp.add_argument("--ignore-certificate-errors")
        driverOtp.add_argument("--disable-extensions")
        driverOtp.add_argument("--disable-gpu")
        driverOtp.add_argument("--log-level=3")
         
        if remote is False:
            self.driver = webdriver.Chrome(options=driverOtp, executable_path=r'chromedriver.exe')
            ch.write("url", self.driver.command_executor._url)
            ch.write("session_id", self.driver.session_id)
            self.switchTab(1)
            self.close()
            self.switchTab(0)
        else:
            sID = ch.get("session_id")
            sURL = ch.get("url")
             
            self.driver = webdriver.Remote(command_executor=sURL, desired_capabilities={})
            self.driver.close()
            self.driver.quit()
            self.driver.session_id = sID
            self.newTab()
            self.switchLastTab()
             
        self.driver.implicitly_wait(30)        
        self.wait = WebDriverWait(self.driver, 30)
Yahoo.py
import CommonChrome as Chrome
import ChromeIni as CI
 
def yahoo():
    ch = CI.Ini()
     
    sID = ch.get("session_id")
    sURL = ch.get("url")
 
    if sID is "" or sURL is "":
        driver = Chrome.ChromeDriver()
    else:
        driver = Chrome.ChromeDriver(True)
 
    driver.redirect("https://www.yahoo.com")
So when the user launch the yahoo.py it will set the session_id and url to .ini file, for reconnect back to existing browser and open as new tab.
However, the user can close the browser and re-open it again, but my session_id and url value in the .ini is not resetting, thus it throw error.

How can I detect if the browser is closed by the user, then it will reset the value of session_id and url in the .ini file?


RE: Selenium how to detect if browser is close? - gahhon - Feb-26-2019

Found my own solution.

Just use try-catch method on WebDriverException and NoSuchWindowException
then create new instance of webdriver.


RE: Selenium how to detect if browser is close? - j.crater - Feb-27-2019

Great to hear you found the solution and thanks for posting it back!