Search code examples
pythongoogle-chromeselenium-webdriverselenium-chromedriverchromium

Error when running Selenium / Chromedriver twice at the same moment


I have a piece of code that is executed "on-demand" taking a screenshot of a locally hosted website (https://) meaning there's a possibility that it runs twice in the same second. It executes perfectly fine hundreds of times if it doesn't execute in the same second, but when it does then one of them gives the error:

Traceback (most recent call last):
  File "/home/trading/python/screenshot_rsi.py", line 37, in <module>
    driver = webdriver.Chrome(options=options)
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__
    super().__init__(
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/chromium/webdriver.py", line 104, in __init__
    super().__init__(
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally.
  (chrome not reachable)
  (The process started from chrome location /snap/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
#0 0x56223f1c45e3 <unknown>
#1 0x56223ee870b7 <unknown>
#2 0x56223eebde55 <unknown>
#3 0x56223eebab81 <unknown>
#4 0x56223ef0547f <unknown>
#5 0x56223eefbcc3 <unknown>
#6 0x56223eec70e4 <unknown>
#7 0x56223eec80ae <unknown>
#8 0x56223f18ace1 <unknown>
#9 0x56223f18eb7e <unknown>
#10 0x56223f1784b5 <unknown>
#11 0x56223f18f7d6 <unknown>
#12 0x56223f15bdbf <unknown>
#13 0x56223f1b2748 <unknown>
#14 0x56223f1b2917 <unknown>
#15 0x56223f1c3773 <unknown>
#16 0x7f28612c1609 start_thread

I have been debugging for days adding and removing options, creating separate --profile-directory, separate --user-data-dir, etc. without being able to figure out why it's happening. If they run only one second apart they execute perfectly fine. I also tried using separate fixed ports in different scripts and that also didn't solve it.

Also tried adding or removing driver.quit() and that didn't help. Also tried creating 2 scripts and naming one driver1 and the other driver2 and that also caused the error.

Here is the Python code I use:

import argparse
import random
import string
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from multiprocessing import Process

# Function to generate a random filename
def generate_random_filename():
    characters = string.ascii_uppercase + string.digits
    return ''.join(random.choice(characters) for _ in range(20))

# Parse command-line arguments
parser = argparse.ArgumentParser(description='Capture a screenshot of page.')
parser.add_argument('--time', type=str, required=True, help='The time.')
args = parser.parse_args()

min_port = 9000 
max_port = 9999
random_port = random.randint(min_port, max_port)

options = webdriver.ChromeOptions()
options.binary_location = '/snap/bin/chromium' 
options.add_argument("--user-data-dir=/tmp-chromium")
options.add_argument("--start-maximized")
options.add_argument(r'--profile-directory=Profile2')
options.add_argument('--no-sandbox')
options.add_argument("--disable-setuid-sandbox")
options.add_argument(f"--remote-debugging-port={random_port}")
options.add_argument('--headless=new')
options.add_argument('--disable-gpu')
options.add_argument('--hide-scrollbars')
driver = webdriver.Chrome(options=options)

driver.set_window_size(2200, 1200)
url = f"https://my.url/page?time={args.time}"
driver.get(url)

try:
    max_wait_time = 10  # Adjust this according to your needs
    WebDriverWait(driver, max_wait_time).until(
        EC.presence_of_element_located((By.CLASS_NAME, "cssSelector"))
    )
    screenshot_filename = generate_random_filename()
    screenshot_path = f"/var/www/html/screenshots/{screenshot_filename}.png"
    driver.save_screenshot(screenshot_path)
    print(screenshot_path)
except Exception as e:
    print(f"An error occurred: {e}")

Any hints or help would be greatly appreciated as at this point I have ran completely out of ideas!

Update 1: Forgot to mention the versions:

  • Chromium 119.0.6045.105 snap
  • ChromeDriver 119.0.6045.105

Solution

  • It appears the issue is with Chromedriver. I installed Geckodriver (Firefox) as someone on SeleniumHQ Github kindly pointed out and the problem can't be replicated.