Forums

Selenium stops working after a while

I have a code that uses selenium to every 20 seconds update a website and read if a given value has changed. It runs in a while loop, in each loop the following is executed:

try:
        # Set up Chrome WebDriver
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--headless") # Run Chrome in headless mode, without opening a window
        chrome_options.add_argument("--disable-gpu")
        driver = webdriver.Chrome(options=chrome_options)

        # Load the web page
        driver.get(url)

        # Wait for the dynamic content to load (adjust the time as needed)
        #.. reads some value...

finally:
        # Quit the WebDriver
        driver.quit()
        return value

After running for a few successful iterations (10 times last time I ran it) the code gives an error. When I stop running the code and try to run the following in the python console:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless") # Run Chrome in headless mode, without opening a window
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options=chrome_options)

The following exception is raised:

selenium.common.exceptions.WebDriverException: Message: unknown error: unable to discover open pages
Stacktrace:
#0 0x5640068b4e89 <unknown>

If I shut down the console and restart the whole thing the same happens. I don't understand why this is happening. How can I prevent it? The code runs perfectly well in my own laptop, so is PythonAnywhere detecting this as spamming or malicious?

How does your Running processes on the bottom of the "Consoles" page look like?

It doesn't look good, it's quite a long list. Would that mean I'm failing to close the drivers I open?

Yes, that sounds most likely. Your code quoted above looks like it's shutting everything down properly in that "finally" statement, but perhaps there's some code path that isn't doing the driver.quit() somewhere?