You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium Chrome Driver最小化/关闭时崩溃致元素列表为空求助

问题描述

我开发了一个用Selenium监控租房网站https://hurenbij.vesteda.com/房源变化的机器人,通过循环检测页面抓取的元素列表变化,刷新页面直到列表出现变更(数量或内容变化)时触发提醒。但遇到问题:最小化/关闭Chrome Driver窗口或切换到VS Code时,机器人会“异常”——抓取的元素列表变成空字符串数组,实际页面内容非空,但日志没有报错信息。

完整代码

def create_driver():
    options = Options()
    options.add_experimental_option("detach", True)
    service = Service(executable_path="/usr/local/bin/chromedriver")
    driver = webdriver.Chrome(service=service, options=options)

    return driver


def login(driver):
    driver.find_element(By.NAME, 'txtEmail').send_keys('XXXXXXXX@gmail.com')
    driver.find_element(By.NAME, 'txtWachtwoord').send_keys('XXXXXXXXX')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
        (By.XPATH, '//*[@id="CybotCookiebotDialogBodyButtonAccept"]'))).click()
    driver.find_element(By.CLASS_NAME, 'btn-secondary').click()


def search_page(driver):
    driver.find_element(By.XPATH, "//a[@href='/zoekopdracht/']").click()


def list_of_woningen(driver):
    woningen = []
    woningLijst = driver.find_elements(By.CLASS_NAME, "stretched-link")

    for x in woningLijst:
        woningen.append(x.text)

    return woningen


def check_for_changes(driver, old_list):
    print("Checking for changes")
    new_list = list_of_woningen(driver)
    if old_list == new_list:
        print("No changes found")
        return False
    else:
        print("Changes found")
        print("Old list: ", old_list)
        print("New list: ", new_list)
        return True


def start_session(driver, url):
    driver.get(url)
    login(driver)
    search_page(driver)
    old_list = list_of_woningen(driver)
    
    while not check_for_changes(driver, old_list):
        WebDriverWait(driver, 10)
        #time.sleep(5)  # 30 seconds
        # a better approach for this, is to make a wait thread, if there are other functionalities for tha program
        driver.refresh()
    else:
        
        print("ALERT!!!")

    # return driver.page_source
if __name__ == '__main__':
    start_session(create_driver(), 'https://hurenbij.vesteda.com/')

运行日志

No changes found
Checking for changes
No changes found
Checking for changes
No changes found
Checking for changes
No changes found
Checking for changes
Changes found
Old list:  ['Bankierbaan 140', 'Boekbinderstraat 3', 'Rozemarijntuin 87', 'Parkzicht 78', 'Brinklaan 138B 1', 'Wijde Wormer 222', 'Sjees 16', 'Fideliolaan 258D', 'Kapittelweg 428', 'Haendelplein 107', 'Haendelplein 55', 'Bankierbaan 234', 'Parkzicht 35', 'Rozemarijntuin 85', 'Rozemarijntuin 101', 'Maan Bastion 170', 'Slachthuisstraat 165', 'Parkzicht 94', 'Slachthuisstraat 145', 'Reimsstraat 9', 'Sir Winston Churchillln 755', 'Sir Winston Churchillln 509']
New list:  ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
ALERT!!!
解决方案

这个问题是Chrome浏览器在后台运行(窗口最小化/不可见)时,触发了懒加载或渲染优化机制,导致元素文本无法正常获取。以下是几个直接有效的修复方案:

1. 启用无头模式并禁用渲染优化

用无头模式运行Chrome,同时添加参数禁用相关优化,确保窗口不可见时也能正常渲染页面:
修改create_driver函数:

def create_driver():
    options = Options()
    options.add_experimental_option("detach", True)
    # 启用新版无头模式
    options.add_argument("--headless=new")
    # 禁用GPU加速避免渲染异常
    options.add_argument("--disable-gpu")
    # 禁用懒加载优化
    options.add_argument("--disable-lazy-loading")
    # 设置固定窗口大小,保证页面渲染完整
    options.add_argument("--window-size=1920,1080")
    service = Service(executable_path="/usr/local/bin/chromedriver")
    driver = webdriver.Chrome(service=service, options=options)

    return driver

2. 改用元素属性获取文本替代text

有时候元素文本未渲染,但innerText或textContent属性可能仍保留内容,修改list_of_woningen函数:

def list_of_woningen(driver):
    woningen = []
    woningLijst = driver.find_elements(By.CLASS_NAME, "stretched-link")

    for x in woningLijst:
        # 优先获取innerText,失败则尝试textContent,最后兜底为空字符串
        text = x.get_attribute("innerText") or x.get_attribute("textContent") or ""
        woningen.append(text.strip())

    return woningen

3. 补充页面加载等待逻辑

当前代码中的WebDriverWait(driver, 10)没有实际等待条件,页面刷新后可能未加载完成就抓取元素,导致文本为空。修改循环内的等待逻辑:

while not check_for_changes(driver, old_list):
    driver.refresh()
    # 等待房源列表元素加载完成
    WebDriverWait(driver, 10).until(
        EC.presence_of_all_elements_located((By.CLASS_NAME, "stretched-link"))
    )
    # 等待页面完全加载完成
    WebDriverWait(driver, 10).until(
        lambda d: d.execute_script("return document.readyState") == "complete"
    )

4. 禁用后台标签页节流(非无头模式适用)

如果需要保留可视化窗口,添加参数禁止Chrome在后台限制资源:

options.add_argument("--disable-background-timer-throttling")
options.add_argument("--disable-backgrounding-occluded-windows")

内容的提问来源于stack exchange,提问作者Amin Aloui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 06:15:19