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

Windows EC2实例中Selenium爬虫断开RDP后无法运行求助

问题描述

我在AWS EC2上创建的实例参数如下:

  • 平台:Windows
  • 版本:Windows_Server-2019-English-Full-Base-2022.07.13
  • 实例类型:t2.micro

需求是在该实例上运行定时爬虫,但遇到以下问题:通过RDP连接启动爬虫后断开连接,浏览器空白不加载页面,控制台抛出异常;仅保持RDP连接时爬虫能正常运行。

相关代码
# undetected_chromedriver == '3.1.5r4'
# selenium == 4.3.0
# python == 3.10.5
# Chrome == Versión 104.0.5112.81 
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import time

import undetected_chromedriver.v2 as uc
from selenium.webdriver.common.keys import Keys


def main(driver):
    driver.get("https://www.google.com/")
    WebDriverWait(driver, LONG_TIME).until(EC.presence_of_element_located((By.TAG_NAME, "form")))
    input = WebDriverWait(driver, LONG_TIME).until(EC.presence_of_element_located((By.TAG_NAME, "input")))
    input.send_keys('stackoverflow')
    input.send_keys(Keys.ENTER)
    search = WebDriverWait(driver, LONG_TIME).until(EC.presence_of_element_located((By.ID, "search")))
    titles = search.find_elements(By.TAG_NAME, "h3")
    for title in titles:
        print(title.get_attribute('textContent'))


if __name__ == '__main__':
    options = uc.ChromeOptions()
    LONG_TIME = 30
    driver = uc.Chrome(options=options, user_data_dir="C:\\temp\\profile")
    while True:
        try:
            main(driver)
        except Exception as e:
            print(e)
        time.sleep(60)
异常信息
Message:
Stacktrace:
Backtrace:
        Ordinal0 [0x012878B3+2193587]
        Ordinal0 [0x01220681+1771137]
        Ordinal0 [0x011341A8+803240]
        Ordinal0 [0x011624A0+992416]
        Ordinal0 [0x0116273B+993083]
        Ordinal0 [0x0118F7C2+1177538]
        Ordinal0 [0x0117D7F4+1103860]
        Ordinal0 [0x0118DAE2+1170146]
        Ordinal0 [0x0117D5C6+1103302]
        Ordinal0 [0x011577E0+948192]
        Ordinal0 [0x011586E6+952038]
        GetHandleVerifier [0x01530CB2+2738370]
        GetHandleVerifier [0x015221B8+2678216]
        GetHandleVerifier [0x013117AA+512954]
        GetHandleVerifier [0x01310856+509030]
        Ordinal0 [0x0122743B+1799227]
        Ordinal0 [0x0122BB68+1817448]
        Ordinal0 [0x0122BC55+1817685]
        Ordinal0 [0x01235230+1856048]
        BaseThreadInitThunk [0x77320419+25]
        RtlGetAppContainerNamedObjectPath [0x778377FD+237]
        RtlGetAppContainerNamedObjectPath [0x778377CD+189]
解决办法
  • 启用Windows自动登录:断开RDP后系统会自动注销用户会话,导致浏览器失去图形环境支持。按Win+R输入netplwiz,取消勾选「要使用本计算机,用户必须输入用户名和密码」,输入登录账号密码后保存,确保断开RDP后会话仍保持活跃。
  • 添加Chrome无头模式配置:修改代码中的ChromeOptions,让浏览器无需图形界面即可运行,避免依赖RDP的会话环境:
    options = uc.ChromeOptions()
    options.add_argument('--headless=new')  # 适配Chrome 104+的无头模式,兼容性更好
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    LONG_TIME = 30
    driver = uc.Chrome(options=options, user_data_dir="C:\\temp\\profile")
    
  • 修改组策略保持会话:打开组策略编辑器(运行gpedit.msc),定位到「计算机配置」→「管理模板」→「Windows组件」→「远程桌面服务」→「远程桌面会话主机」→「会话时间限制」,将「设置断开连接的会话的时间限制」和「设置活动但空闲的远程桌面服务会话的时间限制」均设为已禁用,防止系统自动关闭断开的会话。
  • 检查目录权限:提前创建C:\temp\profile目录,确保运行爬虫的用户对该目录拥有读写权限,避免Chrome无法加载用户配置导致异常。

内容的提问来源于stack exchange,提问作者Andy Ñaca

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 19:24:19