'pysimplegui quits with selenium driver.quit()

I am using pysimpegui to create an interface for a small program that I do in selenium. What happens is that the ui closes when the driver that creates selenium closes. I've noticed that if I comment out driver.quit() the app still works and doesn't quit. How can I avoid this? How to close Chrome driver without crashing pysimplegui ui?

Perhaps it is due to an error in my code, I will leave you with a minimal example right away. I've added the webdriver_manager.chrome module in case you don't have the 'C:/chromedriver.exe' downloaded. This module can be downloaded by disabling the line #driver = webdriver.Chrome(ChromeDriverManager().install()) and commenting out where the dirver is started using the executable.

import os
import time
import PySimpleGUI as sg
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService


chromedriver = r'C:/chromedriver.exe'
service = ChromeService(executable_path=chromedriver)

def rotate():
    driver = webdriver.Chrome(service=service)
    #driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get("https://google.com")
    time.sleep(3)
    driver.quit() #Si comentamos esta línea el error no suceda

sg.theme('Kayak')

layout = [[sg.Text('Archivo base:')],
      [sg.FileBrowse("Cargar",file_types=(("Archivo py", "*.py"),)),sg.In()],
      [ sg.Text(size=(40,1), key='-OUTPUT-'),sg.Button('Buscar'),sg.Button('Salir')]]

layout[-1].append(sg.Sizegrip())
window = sg.Window('Titulo', layout,size=(500,150), right_click_menu_tearoff=True, grab_anywhere=True, resizable=True, margins=(100,100), use_custom_titlebar=True, finalize=True,
                       # scaling=2.0,
                       )
window.set_min_size(window.size)

def main():
  while True:  # Event Loop
    event, values = window.read(timeout=100)
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Salir':
      break
    if event == 'Buscar':
      f=values["Cargar"]
      if f != '':
        dir_name = os.path.dirname(f)
        f_name =os.path.basename(f)
        os.chdir(dir_name)
        # Print the current working directory
        dir_data = os.path.join(dir_name,f_name.split(".")[0])
        if not os.path.exists(dir_data):
          os.mkdir(dir_data)
        try:
          rotate()
        except:
          sg.popup_error(f'Ocurrio un error')
        time.sleep(10)
      else:
        sg.popup_error(f'Por favor cargue un archivo antes de buscar.')
  window.close()

main()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source