【问题标题】:facing an BDD framework error "ElementNotInteractableException"面临 BDD 框架错误“ElementNotInteractableException”
【发布时间】:2021-09-13 16:14:21
【问题描述】:

在这里,我正在尝试使用 BDD python 框架测试我的网站。在我们输入参数时,我遇到了一个错误。错误就像“元素不可交互”。所以下面我粘贴了我的sn-p代码,请告诉我哪里出错了。

.feature 文件

Feature:  Post Ad testing

  Scenario: Login to site valid parameter:
    Given I launch chrome browser
    When i open web page
    And  maximize the window
    And click on login button
    And enter username "username" and password "password"
    And click on submit button
    Then user successfully login to the  web site

.py 文件

@when('enter username "{name}" and password "{pass1}"')
def input_validate(context,name,pass1):
    context.driver.find_element_by_id("semail_login").send_keys(name)
    context.driver.find_element_by_id("password176").send_keys(pass1)

错误

And enter username "username" and password "password" # steps/postAd.py:24
      Traceback (most recent call last):
        File "C:\Users\USER 1\PycharmProjects\behaveproject\venv\lib\site-packages\behave\model.py", line 1329, in run
          match.run(runner.context)
        File "C:\Users\USER 1\PycharmProjects\behaveproject\venv\lib\site-packages\behave\matchers.py", line 98, in run
          self.func(context, *args, **kwargs)
        File "steps\postAd.py", line 26, in input_validate
          context.driver.find_element_by_id("semail_login").send_keys(name)
        File "C:\Users\USER 1\PycharmProjects\behaveproject\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
          self._execute(Command.SEND_KEYS_TO_ELEMENT,
        File "C:\Users\USER 1\PycharmProjects\behaveproject\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
          return self._parent.execute(command, params)
        File "C:\Users\USER 1\PycharmProjects\behaveproject\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
          self.error_handler.check_response(response)
        File "C:\Users\USER 1\PycharmProjects\behaveproject\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
          raise exception_class(message, screen, stacktrace)
      selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
        (Session info: chrome=93.0.4577.63)

html 代码

<form class="myform" action="/signin" method="post" onsubmit="return loginfun()">
                {% csrf_token %}
                <h1 style="font-size:25px;margin-top:3px;margin-bottom:10px;">Log In</h1>
                {{email_lb}}
                <div class="form-group">
                    <input id="semail_login" name="semail" placeholder="E-mail Address" type="email">
                    <span class="input-icon"><i class="fa fa-envelope fa1"></i></span>
                    <p id="email_ls" style="color:red;text-align: left;font-size: 12px;"></p>
                </div>
                <div class="form-group">
                    <input class="password_business9866" id="password176" name="password" placeholder="Password"
                           type="password">
                    <!--                    <p style="color:#DB0038;font-size: 8px;position: relative;top: 9px;"-->
                    <!--                       class='password_business_invalid1`23'></p>-->
                    <span class="input-icon"><i class="fa fa-lock fa1"></i></span>
                </div>

                <div class="form-group" style="height: 32px;">
                    <label class="container12" id="remember_me"></label>
                    <input id="Login_remember" class="input" name="remember_me" type="checkbox">
                    <span style="position: relative;top: -10px;">Remember me</span>
                </div>

                <button class="login-btn" type="submit">Log In
                </button>
                <a class="reset-psw forgotPassword" data-dismiss="modal" data-target="#exampleModal" data-toggle="modal"
                   href="#">Forgot Password?</a>
                <p>Don't have account?<a class="" data-dismiss="modal" data-target="#myModal_s" data-toggle="modal"
                                         href="#" style="color:#DB0038;">&nbsp;Sign Up</a></p>
            </form>

【问题讨论】:

  • 能否提供html或页面链接?
  • 您好,我已添加 html 代码,请查看
  • 嗨,你有没有尝试过stackoverflow.com/questions/44119081/…这里的一些解决方案?
  • 我们需要在浏览器中看到渲染的 HTML。这看起来像您从服务器端或客户端模板复制和粘贴的 HTML。对于 Selenium,重要的是浏览器看到的 HTML。您可以从浏览器的 HTML 检查器中复制它。

标签: python selenium bdd


【解决方案1】:

这可能有点棘手,因为您的功能文件在屏幕上发生的事情和您的测试正在做的事情之间提供了另一层间接性。您可能可以应用一两个修复程序。您可能需要同时申请两者。

  1. 等待“登录”按钮失效。 上一步And click on login button 可能会导致页面卸载,并加载登录页面。在单页应用程序的情况下,当前页面会使用登录屏幕进行更新。您可能只需要等待“登录”按钮“过时”,这意味着该元素不再附加到浏览器中的文档树:

    # In the step definition for 'Given click on login button'
    wait = WebDriverWait(context.driver, 30)
    login_button = context.driver.find_element((By.WHATEVER, "..."))
    login_button.click()
    wait.until(EC.staleness_of(login_button)
    
  2. 等待用户名字段可点击。当页面转换到登录屏幕时,无论这是由于新页面加载还是当前页面正在更新,请尝试等待用户名下一步中的字段变为可点击,这意味着您可以使用 Selenium 与它进行交互。

    @when('enter username "{name}" and password "{pass1}"')
    def input_validate(context,name,pass1):
        wait = WebDriverWait(context.driver, 30)
        wait.until(EC.element_to_be_clickable(By.ID, "semail_login")).send_keys(name)
        wait.until(EC.element_to_be_clickable(By.ID, "password176")).send_keys(pass1)
    

您可能需要同时执行这两项操作。您可能需要等待“登录”按钮在上一步中失效,并且您可能还需要等待用户名和密码字段在下一步中可单击。

【讨论】:

    猜你喜欢
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多