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

使用Python Behave Selenium运行自动化代码时Chrome闪退问题求助

Behave + Selenium 自动化测试Chrome闪退问题排查与解决

问题背景

  • 技术栈:Python + Visual Studio Code + Behave + Selenium WebDriver
  • 异常现象:Chrome浏览器启动后立即闪退,未加载目标URL;移除Behave的Given/When/Then装饰器及对应函数后,代码可正常运行;VS Code及终端无任何报错信息
  • 需求:实现Behave自动化测试并集成Allure报告

原问题代码

from behave import *
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

service = Service(executable_path="chromedriver.exe")
driver = webdriver.Chrome(service=service)

@given('User accesses website')
def accessWebsite(context):
    driver.get("<website URL here")  # 缺少闭合引号

@when('User enters valid registered email')
def enterUsername(context):
    driver.find_element(By.ID, "username").send_keys("test@gmail.com")

@when('User enters valid registered password')
def enterPassword(context):
    driver.find_element(By.XPATH, '//*[@id="root"]/div/div/div/div[2]/div/div/div/div[2]/form/div/div[2]/div/div/div[1]/input').send_keys("ABc123$%")

@when('User presses Login button')
def pressEnter(context):
    driver.find_element(By.XPATH, '//*[@id="root"]/div/div/div/div[2]/div/div/div/div[2]/form/div/div[3]/button').click()

@then('User is redirected to website main page')
def verifyWorkspacesHeader(context):
    driver.implicitly_wait(10)
    driver.find_element(By.XPATH, '//*[@id="root"]/div/div[2]/div[1]/p')

def after_all(context):
    time.sleep(10)
    driver.quit()

排查与解决方案

1. 修复隐性语法错误

原代码中driver.get("<website URL here")缺少闭合双引号,会导致Behave执行流程隐性终止,直接触发浏览器闪退。修正为:

driver.get("<website URL here>")

2. 正确绑定Driver到Behave Context

全局初始化Driver会导致Behave步骤函数上下文丢失,应在before_all钩子中初始化Driver并绑定到context对象,所有步骤通过context.driver访问:

from behave import *
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

def before_all(context):
    service = Service(executable_path="chromedriver.exe")
    context.driver = webdriver.Chrome(service=service)

@given('User accesses website')
def accessWebsite(context):
    context.driver.get("<website URL here>")  # 已修正引号

@when('User enters valid registered email')
def enterUsername(context):
    context.driver.find_element(By.ID, "username").send_keys("test@gmail.com")

@when('User enters valid registered password')
def enterPassword(context):
    context.driver.find_element(By.XPATH, '//*[@id="root"]/div/div/div/div[2]/div/div/div/div[2]/form/div/div[2]/div/div/div[1]/input').send_keys("ABc123$%")

@when('User presses Login button')
def pressEnter(context):
    context.driver.find_element(By.XPATH, '//*[@id="root"]/div/div/div/div[2]/div/div/div/div[2]/form/div/div[3]/button').click()

@then('User is redirected to website main page')
def verifyWorkspacesHeader(context):
    context.driver.implicitly_wait(10)
    context.driver.find_element(By.XPATH, '//*[@id="root"]/div/div[2]/div[1]/p')

def after_all(context):
    time.sleep(10)
    context.driver.quit()

3. 启用Behave详细日志排查隐藏错误

运行Behave时添加参数输出详细日志,捕获隐性报错:

behave --verbose --no-capture
  • --verbose:输出执行过程的详细日志
  • --no-capture:禁用输出捕获,确保所有错误信息直接显示

4. 检查Feature文件步骤匹配

确保Feature文件中的步骤描述与代码中Given/When/Then装饰器的文本完全一致(包括大小写、空格、标点),步骤不匹配会导致Behave跳过执行,直接触发after_all关闭浏览器。

5. Allure报告集成步骤

  1. 安装依赖:
pip install allure-behave
  1. 运行测试并生成报告数据:
behave -f allure_behave.formatter:AllureFormatter -o ./allure-results
  1. 生成并查看HTML报告:
allure serve ./allure-results

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 17:52:47