Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Selenium how to detect if browser is close?
#1
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?
Quote
#2
Found my own solution.

Just use try-catch method on WebDriverException and NoSuchWindowException
then create new instance of webdriver.
Quote
#3
Great to hear you found the solution and thanks for posting it back!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  webdriver.remote to connect back existing browser without open new browser? gahhon 6 1,819 Feb-26-2019, 03:53 PM
Last Post: gahhon
  Selenium, how to open url in current browser? Dzonzi 1 2,086 Jul-18-2018, 02:52 PM
Last Post: snippsat
  Error in Selenium: CRITICAL:root:Selenium module is not installed...Exiting program. AcszE 1 1,122 Nov-03-2017, 08:41 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)