首页 > 编程问答 >python selenium 行为错误:AttributeError:“Context”对象没有属性“driver”

python selenium 行为错误:AttributeError:“Context”对象没有属性“driver”

时间:2024-07-23 07:19:19浏览次数:8  
标签:python selenium-webdriver bdd python-behave

我正在使用 python selenium 与Behavior包一起工作。

这是代码:

@given('the user is on the login page')
def step_given_user_on_login_page(context):
    PATH = 'C:/Users/PycharmProjects/ui_test/chromedriver-win32/chromedriver.exe'
    context.driver = webdriver.Chrome(PATH, options=chrome_options)
    context.driver.set_window_position(2000, 0)
    context.driver.maximize_window()
    context.driver.get("http://127.0.0.1:5000/cases")

@when('the user enters valid credentials')
def step_when_user_enters_valid_credentials(context):
    login(context.driver, username, password)

@when('clicks on the login button')
def step_when_clicks_login_button(context):
    # Assuming login function handles the login button click
    pass

@then('the user should be redirected to the cases page')
def step_then_user_redirected_to_cases_page(context):
    assert "cases" in context.driver.current_url

代码运行良好,直到这一行:

assert "cases" in context.driver.current_url

错误是:

 AttributeError: 'Context' object has no attribute 'driver'

我很困惑为什么第一个context.driver有效:

login(context.driver, username, password)

但是第二个 context.driver 失败了

在 context.driver.current_url 中断言“cases”

有朋友可以帮忙吗?


问题在于,可能没有在 step_then_user_redirected_to_cases_page 函数中初始化或传递 context.driver 。 尽管 context 对象在不同的步骤函数之间共享,但是 context.driver 属性只会在显式设置它的函数中可用。

可以通过以下两种方法解决此问题:

方法 1:将 driver 作为参数传递

context.driver 作为参数传递给需要它的函数:

@when('the user enters valid credentials')
def step_when_user_enters_valid_credentials(context):
    login(context.driver, username, password)

@when('clicks on the login button')
def step_when_clicks_login_button(context):
    # Assuming login function handles the login button click
    pass

@then('the user should be redirected to the cases page')
def step_then_user_redirected_to_cases_page(context):
    assert "cases" in context.driver.current_url

修改的步骤定义,将 context.driver 传递给需要它的函数:

@when('the user enters valid credentials')
def step_when_user_enters_valid_credentials(context):
    login(context.driver, username, password)

@when('clicks on the login button')
def step_when_clicks_login_button(context):
    click_login_button(context.driver)  # 假设 click_login_button 函数处理登录按钮点击

@then('the user should be redirected to the cases page')
def step_then_user_redirected_to_cases_page(context):
    assert "cases" in context.driver.current_url

方法 2:使用 context.driver 初始化所有需要它的函数

在每个需要使用 context.driver 的步骤函数中,添加初始化代码:

@when('the user enters valid credentials')
def step_when_user_enters_valid_credentials(context):
    context.driver = context.driver  # 这行代码确保 context.driver 在该函数中可用
    login(context.driver, username, password)

@when('clicks on the login button')
def step_when_clicks_login_button(context):
    context.driver = context.driver
    # Assuming login function handles the login button click
    pass

@then('the user should be redirected to the cases page')
def step_then_user_redirected_to_cases_page(context):
    context.driver = context.driver
    assert "cases" in context.driver.current_url

请注意,这两种方法都需要在适当的地方传递或初始化 context.driver 。 选择最适合的代码结构和个人偏好的方法。

标签:python,selenium-webdriver,bdd,python-behave
From: 78781031

相关文章

  • python 脚本中的路点用于处理大数据集
    我编写了一个脚本,将一堆来自api的请求写入csv文件。该api中有数千个请求,并且在结束循环/退出程序之前永远不会结束。如何合并航路点,以便如果再次发生停顿,它会自动继续前进并最终打印所有请求?我尝试了一些不起作用的方法,但我不知道下一步该怎么做。以下是使用航路点......
  • Python 中的 SSL 模块不可用(在 OSX 上)
    我在OSX10.13上的virtualenv中运行时遇到问题。我已经运行了pipinstall并且路径brewinstallopenssl指向/usr/local/include/openssl有谁知道如何解决这一问题?在我重新安装../opt/openssl/include/openssl使用python后,这种......
  • AWS Elastic Beanstalk chown PythonPath 错误
    我正在AWS的elasticbeanstalk上部署一个Web应用程序,遇到了同样的错误:[StageApplication].Stoprunningthecommand.Error:chown/var/app/staging/venv/bin/python:nosuchfileordirectory.我在我的环境配置中看到属性:PYTHONPATH:/var/......
  • Python:支持索引的内存对象数据库?
    我正在做一些数据整理,如果我可以将一堆字典放入内存数据库中,然后对其运行简单的查询,这会简单得多。例如,类似:people=db([{"name":"Joe","age":16},{"name":"Jane","favourite_color":"red"},])over_16=db.filter(age__g......
  • 如何构建一维数组的二维数组的特定 Python 结构?
    如何构建一维数组(即行向量)的二维数组的特定结构以满足特定我正在维护的遗留程序的结构?我可以在此结构中生成正确的内容all_measurements[:12]array([[0.,0.,0.,2.],[0.02,0.334,0.04,2.24],[0.04,0.668,0.08,2.48],...........
  • 如何使用 Python Flask 将新的咖啡馆(元素)添加到数据库(SQLite)?
    这是我的代码:@app.route("/add",methods=["POST"])defpost_new_cafe():new_cafe=Cafe(name=request.form.get("name"),map_url=request.form.get("map_url"),img_url=request.form.get("img......
  • 使用 tkinter 为 python 创建 GUI 时如何解决语法错误?
    我是一名Python初学者,决定使用tkinter制作一个小型GUI,该GUI接受用户(潜在餐馆)的3个输入,然后单击按钮后随机输出其中一家餐馆。我不断收到语法错误,并认为它与我的buttonfunc或调用它的命令有关。此代码尚未包含在GUI上输出餐厅的任何位置。任何帮助将不胜感激#Pyth......
  • 在 python 中打开 gnome 终端立即显示为僵尸
    作为背景,我正在编写一个脚本来训练多个pytorch模型。我有一个训练脚本,我希望能够在gnome终端中作为子进程运行。这样做的主要原因是我可以随时关注训练进度。如果我可能有多个GPU,我想在单独的窗口中多次运行我的训练脚本。为了实现这一点,我一直在使用popen。以下代码用于打......
  • python threading.Condition 的意外行为
    我正在尝试同步多个线程。我期望使用threading.Condition和threading.Barrier时的脚本输出大致相同,但事实并非如此。请解释一下为什么会发生这种情况。一般来说,我需要线程在一个无限循环中执行工作(一些IO操作),但是每个循环都是以主线程的权限开始的,而权限是仅在......
  • Python - 逆透视数据框
    我有一个按日期时间索引的表,每个日期时间都有多个层(中心和交货间隔):日期时间中心交货间隔结算点价格2024-01-0101:00:00休斯顿中心1......