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

关闭RDP后Chrome Webdriver无法与页面元素交互的问题求助

问题原因与解决方案

核心问题

关闭RDP后Azure Windows虚拟机的桌面会话会被自动挂起/注销,导致Chrome失去GUI环境支持,同时原代码依赖keyboard模块模拟桌面输入、大量使用time.sleep的不稳定操作,共同引发ElementNotInteractableException错误。


一、解决RDP断开后会话挂起问题

方法1:修改组策略

  1. 运行gpedit.msc打开组策略编辑器
  2. 导航到:计算机配置 → 管理模板 → Windows组件 → 远程桌面服务 → 远程桌面会话主机 → 会话时间限制
  3. 启用「设置断开连接的会话的时间限制」,设置为从不
  4. 启用「设置活动但空闲的远程桌面服务会话的时间限制」,设置为从不

方法2:命令行修改注册表

执行以下cmd命令:

reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DisableCAD /t REG_DWORD /d 1 /f
reg add "HKLM\System\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
reg add "HKLM\System\CurrentControlSet\Control\Terminal Server\Session" /v MaxDisconnectionTime /t REG_DWORD /d 0 /f

方法3:用tscon保留会话断开RDP

  1. 执行quser查看当前会话ID
  2. 执行tscon <会话ID> /dest:console,断开RDP后会话会保留在虚拟机控制台

二、配置Chrome无头模式适配无桌面环境

修改Chrome启动参数,让它在无桌面环境下正常运行:

options = Options()
# 启用新版无头模式
options.add_argument("--headless=new")
# 禁用GPU渲染
options.add_argument("--disable-gpu")
# 设置固定窗口尺寸,确保元素定位正常
options.add_argument("--window-size=1920,1080")
# 禁用沙箱(Azure VM环境必备)
options.add_argument("--no-sandbox")
# 禁用/dev/shm使用,避免资源不足
options.add_argument("--disable-dev-shm-usage")
# 保留已登录的Chrome用户数据
options.add_argument(r"--user-data-dir=C:\\Users\\admin23\\AppData\\Local\\Google\\Chrome\\User Data\\")

三、优化代码替换不稳定操作

  1. 移除keyboard模块,改用Selenium原生文件上传
    Chrome文件选择框支持直接用send_keys传入文件路径,无需模拟键盘:
# 替换原有的elem.click() + keyboard操作
file_input = driver.find_element("xpath", '//*[@id="select-files-button"]')
# 传入视频文件的完整绝对路径
file_input.send_keys("C:\\Users\\admin23\\Desktop\\your_video.mp4")
  1. 用显式等待替代time.sleep
    显式等待会等待元素加载完成后再执行操作,比固定等待更稳定:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# 示例:等待上传图标可点击
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="upload-icon"]/tp-yt-iron-icon'))
).click()

完整优化后代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

options = Options()
# 无头模式配置
options.add_argument("--headless=new")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
# 保留已登录用户数据
options.add_argument(r"--user-data-dir=C:\\Users\\admin23\\AppData\\Local\\Google\\Chrome\\User Data\\")
options.page_load_strategy = "normal"

driver = webdriver.Chrome(options=options)
driver.get("https://studio.youtube.com")

# 点击上传图标
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="upload-icon"]/tp-yt-iron-icon'))
).click()

# 上传视频文件
file_input = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//*[@id="select-files-button"]'))
)
file_input.send_keys("C:\\Users\\admin23\\Desktop\\your_video.mp4")

# 等待上传初始化并点击下一步
WebDriverWait(driver, 30).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="next-button"]/div'))
).click()

# 设置视频公开状态
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="text-container"]/div'))
).click()
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="radio-on"]'))
).click()
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="save-button"]/div'))
).click()

# 连续点击三次下一步
for _ in range(3):
    WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="next-button"]/div'))
    ).click()

# 设置隐私为公开
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="privacy-radios"]/tp-yt-paper-radio-button[3]'))
).click()

# 完成上传
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="done-button"]'))
).click()

# 关闭完成弹窗
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="secondary-action-button"]/div'))
).click()

driver.quit()

额外注意事项

  • 确保Chrome版本与ChromeDriver版本完全匹配,避免兼容性问题
  • 如果用户数据目录使用异常,可创建新的Chrome配置文件,手动登录YouTube Studio后再用脚本调用
  • 先在无头模式下本地测试脚本(无需RDP连接),确认正常后再断开RDP验证

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 23:10:14