Home > Enterprise >  Selenium - How to open a browser with the driver once it's been closed
Selenium - How to open a browser with the driver once it's been closed

Time:11-28

I have a Start button which once pressed, will navigate to a URL. The button then turns into a Stop button which will close the browser. Once the Stop button is pressed, it turns back into a Start button which I want to open the browser again. The problem is that I'm getting the error once the driver has been closed and the Start button is pressed again: selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id

I was wondering if it was possible to do this? I tried putting a try/exception which declares a new driver object but this doesn't seem to work.

CODE:

def start_stop_button_clicked(button_status = "Start"):
    if button_status == "Start":
        try:
            driver.get("https://basketball-reference.com")
            start_stop_button.configure(text = "STOP", fg = "red", command = lambda: start_stop_button_clicked("Stop"))
        except InvalidSessionIdException:
            driver = webdriver.Chrome("C:\\Users\\draze\\Documents\\Python Scripts\\chromedriver\\chromedriver.exe")
            driver.get("https://basketball-reference.com")
            start_stop_button.configure(text = "STOP", fg = "red", command = lambda: start_stop_button_clicked("Stop"))
    elif button_status == "Stop":
        driver.close()
        start_stop_button.configure(text = "START", fg = "green", command = lambda: start_stop_button_clicked("Start"))

start_stop_button = Button(root_window, text = "START", fg = "green", command = lambda: start_stop_button_clicked("Start"))
start_stop_button.grid(column = 0, row = 1)

root_window.mainloop()

UPDATE:

I tried turning the web driver into an array so that every time the Stop button is pressed, it closes the driver and then increments the index by one. The first time I run the script everything looks good, but when I press the Stop button, it closes the driver and then the script freezes. Here is the code:

start_stop_button = Button(root_window, text = "START", fg = "green", command = lambda: start_stop_button_clicked("Start", 0))
start_stop_button.grid(column = 2, row = 1, padx = 10, pady = 5)

def start_stop_button_clicked(button_status = "Start", driver_instance = 0):
    if button_status == "Start":
        driver.append(webdriver.Chrome("C:\\Users\\draze\\Documents\\Python Scripts\\chromedriver\\chromedriver.exe"))
        try:
            url = url_textfield.get()
            if url[:8] == "https://":
                driver[driver_instance].get(url)
            elif url[:7] == "http://":
                driver[driver_instance].get(url)
            else:
                driver[driver_instance].get("https://"   url)
            start_stop_button.configure(text = "STOP", fg = "red", command = lambda: start_stop_button_clicked("Stop", driver_instance))
        except InvalidSessionIdException:
            #driver = webdriver.Chrome("C:\\Users\\draze\\Documents\\Python Scripts\\chromedriver\\chromedriver.exe")
            driver.get("https://basketball-reference.com")
            start_stop_button.configure(text = "STOP", fg = "red", command = lambda: start_stop_button_clicked("Stop", driver_instance))
    elif button_status == "Stop":
        driver[driver_instance].close()
        driver_instance = driver_instance   1
        start_stop_button.configure(text = "START", fg = "green", command = lambda: start_stop_button_clicked("Start", driver_instance))



root_window.mainloop()

CodePudding user response:

So after changing the driver into an array of drivers, I also had to change driver[instance].close() to driver[instance].quit() and it stopped freezing! Thank you for the suggestion @AbiSaran!

  • Related