同时运行两次 Selenium / Chromedriver 时出错

Error when running Selenium / Chromedriver twice at the same moment

提问人:theplau 提问时间:11/8/2023 最后编辑:theplau 更新时间:11/9/2023 访问量:62

问:

我有一段代码是“按需”执行的,截取本地托管网站(https://)的屏幕截图,这意味着它有可能在同一秒内运行两次。如果它没有在同一秒内执行,它会执行数百次,但是当它执行时,其中一个会给出错误:

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

我已经调试了好几天,添加和删除选项,创建单独的,单独的等,但无法弄清楚为什么会发生这种情况。如果它们只相隔一秒,它们就会执行得非常好。我还尝试在不同的脚本中使用单独的固定端口,但这也没有解决它。--profile-directory--user-data-dir

还尝试添加或删除,但没有帮助。还尝试创建 2 个脚本并命名一个 driver1 和另一个 driver2,这也导致了错误。driver.quit()

这是我使用的 Python 代码:

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}")

任何提示或帮助将不胜感激,因为在这一点上,我已经完全没有想法了!

更新1:忘了提及版本:

  • Chromium 119.0.6045.105 快照
  • Chrome驱动程序 119.0.6045.105
python google-chrome selenium-chromedriver chromium

评论

0赞 pcalkins 11/11/2023
不要以为您可以同时使用相同的配置文件。也不确定为什么要设置调试端口,但如果对每个端口都使用相同的端口,您也可能会看到问题。如果未设置这两个选项中的任何一个,驱动程序将使用空闲端口,并创建新的临时配置文件。

答:

0赞 theplau 11/9/2023 #1

问题似乎出在Chromedriver上。我安装了 Geckodriver (Firefox),正如 SeleniumHQ Github 上的某个人善意指出的那样,该问题无法复制。