Selenium自动登录脚本添加账号密码后页面崩溃报错求助
解决Selenium Chrome驱动页面崩溃(session deleted because of page crash)问题
这个错误通常由Chrome与驱动版本不匹配、内存不足、页面未加载完成就操作或沙箱配置不全导致,以下是针对性解决方法:
1. 强制匹配ChromeDriver与Chrome浏览器版本
虽然webdriver-manager会自动安装对应驱动,但偶尔会出现版本偏差。你的Chrome版本是119.0.6045.200,可以手动指定版本安装:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By my_username = "my_user_name" my_password = "my_secret_passwd" # 指定ChromeDriver版本与你的Chrome完全一致 driver = webdriver.Chrome(service=Service(ChromeDriverManager(version="119.0.6045.200").install()))
2. 补充Chrome启动参数,解决沙箱与内存问题
仅加no-sandbox可能不够,添加以下参数解决共享内存限制、GPU渲染兼容性等问题:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By my_username = "my_user_name" my_password = "my_secret_passwd" chrome_options = Options() chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") # 解决内存不足导致的页面崩溃 chrome_options.add_argument("--disable-gpu") # 禁用GPU渲染,避免兼容性问题 chrome_options.add_argument("--incognito") # 用无痕模式运行,减少缓存干扰 driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) driver.get("https://remotecontrol.at/")
3. 添加显式等待,确保元素加载完成再操作
直接查找元素可能因为页面未完全加载导致状态异常,用WebDriverWait等待元素可交互后再操作:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC my_username = "my_user_name" my_password = "my_secret_passwd" chrome_options = Options() chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) driver.get("https://remotecontrol.at/") # 等待用户名输入框可点击,最长等待10秒 username = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "login")) ) username.send_keys(my_username) # 等待密码输入框可点击 passwd = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.NAME, "password")) ) passwd.send_keys(my_password) # 后续登录操作也建议用显式等待 # login_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "login"))) # login_button.click() # 用quit()代替close(),彻底终止Driver进程 driver.quit()
4. 清理系统资源与残留进程
- 关闭其他占用大量内存的程序,给Chrome足够运行空间;
- 每次脚本结束用
driver.quit()代替driver.close(),确保Driver进程完全关闭,避免残留进程占用资源。
内容的提问来源于stack exchange,提问作者Paul Vienna
相关产品推荐
相关产品推荐

