“连接池已满,正在丢弃连接”警告 Selenium 在 Django 中的 Celery 任务上

问题描述 投票:0回答:2

所以我在 Django 上使用 Celery 来安排任务。我的任务之一是使用 Selenium 生成一个驱动程序,用于驱动 Selenium Grid 上的 chrome 节点。

该任务还会生成一个线程,该线程将驱动程序作为参数,其唯一目标是关闭页面上的弹出窗口。它基本上无限运行,寻找弹出窗口关闭

options = webdriver.ChromeOptions()
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
options.add_experimental_option("prefs", 
                                    {"profile.content_settings.exceptions.clipboard": {"[*.]mywebsite.com,*": 
                                                                                       {'last_modified': (time.time()*1000), 'setting': 1}}})

    driver = webdriver.Remote(
            command_executor='http://localhost:4444/wd/hub',
            options=options
        )

stop_event= threading.Event()

    popup_thread = threading.Thread(
                target=close_popup_window,
                args=(driver,
                      stop_event)
            )

    popup_thread.daemon = True
    popup_thread.start() 

Thread的作用是

def close_popup_window(driver, stop_event):
    list_popup = ["/html/body/div[3]/div[3]/div/div[1]/div[1]/div[2]/button",
                  "//*[@id=\"max-width-dialog-title\"]/h2/button",
                  "/html/body/div[6]/div[3]/div/div[1]/button",
                  "/html/body/div[5]/div[3]/div/div[1]/button",
                  "/html/body/div[3]/div[3]/div/div[1]/button",
                  "/html/body/div[4]/div[3]/div/div[1]/button",
                  "//*[@id=\"onetrust-accept-btn-handler\"]",
                  "//*[@id=\"max-width-dialog-title\"]/button",
                  ]
    while not stop_event.is_set():
        for popup in list_popup:
            try:
                close_button = driver.find_element(By.XPATH, popup)
                close_button.click()
            except:
                pass

任务的主程序,启动线程的程序,也使用驱动程序

text_box = driver.find_element(By.XPATH,"//*[@id=\"inputText\"]")
text_box.send_keys(first_half)

线程似乎生成了警告

connection pool is full, discarding connection
。一切似乎工作正常,我只是在 Celery 控制台上弹出这个警告,但我不明白为什么会生成它以及如何修复它。

我已经尝试过了

selenium-webdriver celery python-multithreading
2个回答
0
投票

按照建议

WebDriverWait
帮助解决了问题

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

我用过例如

text_box = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,"the_actual_X_path")))

copy_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,
                                    "the_actual_X_path")))

0
投票

你可以在webdriver初始化中使用arg keep_alive=False,这是因为selenium使用connectionPool管理连接,如果keep_alive为True,它只使用webdriver实例中唯一的connectionPool,所以当多线程操作默认设置的单个connectionPool时maxSize 1,线程1获取连接1,线程2无法获取连接并创建新连接,当它们的请求结束并将连接放回到池中时,发生错误,这两个连接将被放入1大小的池中,你可以看到详细信息在来自 urllib3 和 selenium 的 ConnectionPool.py 中

© www.soinside.com 2019 - 2024. All rights reserved.