Need advice on python (3.10) + selenium (4.15.2)

95 Views Asked by At

I'm not an expert on Python nor Selenium despite having some simple projects working combining them. I already have a working solution to my problem, however I'd like you to comment on it. I have simplified the code to make it easier for you to focus on the important aspects.

I have an AutoIt script that calls (RunWait) a compiled (pyinstaller) version of my python code which opens a browser through the Selenium API, login, perform some actions on the site and logout. Since this is done many times I wanted to keep a browser session active to save time. To do this I save the command_executor_url and session_id to a file and retrieve it every time I run this script or create a new instance if necessary.

The key is to let the main process exit (to make RunWait return) but, keep the driver (chromedriver) running (and listening to incoming connections), so the main process creates a daemon process.

This is the entry point. (My thoughts are in the comments)

if __name__ == "__main__":
    freeze_support()

    # connect to existing instance or create a new one
    driver = get_driver()

    # login, perform some actions, logout through Selenium API
    # driver.get(...

These are the main functions.

def create_instance(sem):
    driver = webdriver.Chrome(options = get_chrome_options())
    driver.implicitly_wait(10)

    write_instance_data(driver, PATH_TO_INSTACE_DATA)

    # Instance created, let parent process connect to it.
    sem.release()

    # This is awful and I don't undestand what happens here...
    # This function seems to terminate despite having this loop.
    # Maybe, whenever the parent exits, this child exit, even when daemon is set to True.
    # However chromedriver keep runnning.
    while True:
        time.sleep(60)

def get_driver():
    while True:
        try:
            command_executor_url, session_id = read_instace_data(PATH_TO_INSTACE_DATA)

            # Might throw an exception 
            driver = webdriver.Remote(command_executor_url, options = get_chrome_options())

            # Remote opens a new window. I don't need it.
            driver.close()

            driver.session_id = session_id

            # If the window I do need is closed, just create a new instance.
            # I don't know (yet) whether I can open a new window if it is the case.
            if len(driver.window_handles) == 0:
                raise Exception("No window available")

            break

        except Exception as e:
            sem = Semaphore(0)

            p = Process(target = create_instance, args = (sem,), daemon = True)
            p.start()

            # Wait until instance is created.
            sem.acquire()

    return driver

0

There are 0 best solutions below