【问题标题】:Python 3 KeyError: "'__name__' not in globals"Python 3 KeyError:“'__name__'不在全局变量中”
【发布时间】:2019-07-14 20:05:29
【问题描述】:

当我从步骤 python 文件导入页面对象模型时,我得到一个 KeyError:“'name' not in globals”。 我正在使用 python 3。

功能/步骤/tutorial.feature

import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from behave import *


from ..lib.pages.login_page import LoginPage

use_step_matcher("re")


@given("user is lead to the login page")
def step_impl(context):
    """
    :type context: behave.runner.Context
    """
    context.browser.get("https://www.phptravels.net/login")


@when("I log in")
def step_impl(context):
    """
    :type context: behave.runner.Context
    """
    page = LoginPage(context)
    page.login()

features/lib/pages/login_page.py

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec


class LoginPage:
    locator_dictionary = {
        "email_field": (By.NAME, 'username'),
        "password_field": (By.ID, 'password'),
        "login_button": (By.CSS_SELECTOR, '.btn.btn-action.btn-lg.btn-block.loginbtn'),
        "accept_cookies": (By.ID, 'cookyGotItBtn')
    }

    def __init__(self, context):
        self.browser = context.browser

    def login(self, username="xxxxx", passwd="xxxxx"):
        b = self.browser

        WebDriverWait(b, 10).until(
            ec.visibility_of_element_located((By.ID, "cookyGotItBtn"))).click()

        b.find_element_by_name('username').send_keys(username)
        b.find_element_by_id('password').send_keys(passwd)
        b.find_element_by_css_selector('.btn.btn-action.btn-lg.btn-block.loginbtn').click()

features/tutorial.feature

Feature: showing off behave

  Background: user is lead to the login page
    Given user is lead to the login page

  Scenario: login invalid
    When I log in

我的目录结构是:

E:features\tutorial.feature
E:features\__init__.py
E:features\lib\__init__.py
E:features\lib\pages\__init__.py
E:features\lib\pages\login_page.py
E:features\steps\__init__.py
E:features\steps\tutorial.py

文件“steps/tutorial.py”,第 8 行,在 从 ..lib.pages.login_page 导入 LoginPage KeyError:“'name' 不在全局变量中”

【问题讨论】:

    标签: python python-3.x selenium cucumber python-behave


    【解决方案1】:

    你很可能需要将包名放在导入路径中:

    from features.lib.pages.login_page import LoginPage
    

    【讨论】:

    • 感谢您的回复,虽然我试过了,但得到以下信息: ModuleNotFoundError: No module named 'features' 我不知道为什么
    • 您需要在路径中添加功能。我猜你用的是windows,所以我相信``` import sys; sys.path.append("e:\\features") ``` 之类的对你有用
    • 我正在使用 macbook
    • 好吧,e:\\ 不适合你,因为你使用的是 macbook。我的意思是找到您的工作目录的正确路径并将其添加到您的 sys.path。例如,如果 /home/xfx/features 是您的工作目录,请将 /home/xfx 添加到您的 sys.path 和 from features.lib... import ...
    • 这是我到 features 目录的路径:'/Users/fran/PycharmProjects/prueba1/features',我将它添加到 sys.path,我打印了它并且它在那里但仍然不起作用,太令人沮丧了。
    【解决方案2】:

    你能确认你没有相关的进口吗?

    作为一种缓解措施,您可以尝试在使用 LoginPage 之前导入它。

    @when("I log in")
        def step_impl(context):
        """
        :type context: behave.runner.Context
        """
        from ..lib.pages.login_page import LoginPage
        page = LoginPage(context)
        page.login()
    

    【讨论】:

    • 这是我唯一的导入。现在我把它放在它使用之前,但我仍然得到 KeyError:“'name' not in globals”。
    • 如果我将它作为 '''from features.lib.pages.login_page import LoginPage''' 导入,我会得到一个 ModuleNotFoundError: No module named 'features'
    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 2014-11-07
    • 2012-03-11
    • 2020-10-12
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 2011-07-28
    相关资源
    最近更新 更多