1

我有一个示例 BDD Python 行为代码。当我运行行为 test.feature 时,主页会打开,但随后出现以下错误:

'Context' object has no attribute 'find_element'

完整的错误是:

Scenario Outline: visit test and search for product -- @1.1 By product  # test.feature:27
Given we are on the test homepage                                     # steps\steps.py:37
When we enter "<product>" in the search field                           # steps\steps.py:43
  Traceback (most recent call last):
    File "C:\Python27\lib\site-packageehave\model.py", line 1456, in run
      match.run(runner.context)
    File "C:\Python27\lib\site-packageehave\model.py", line 1903, in run
      self.func(context, *args, **kwargs)
    File "steps\steps.py", line 50, in step
      search_field = context.find_element(By.XPATH, 'id("twotabsearchtextbox")')
  unner.py", line 214, in __getattr__eehave
      raise AttributeError(msg)
  AttributeError: 'Context' object has no attribute 'find_element'

我的代码片段是:

测试功能:

Feature: testing test

Scenario Outline: visit test and search for product
    Given we are on the test homepage
    When we enter "<product>" in the search field
    And we click the search button
    Then the list of products are displayed

    Examples: By product
        | Forumla One |
        | PS4         |
        | Headphones  |

步骤.py

from behave   import given, when, then
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

@given ('we are on the test homepage')
def step(context):
    context.browser.visit()


@when ('we enter "{product}" in the search field')
def step(context, product):
   search_field = context.find_element(By.XPATH, 'id("twotabsearchtextbox")')
   search_field.send_keys(product)

@when ('we click the search button')
def step(context):
   pass

@then ('the list of products are displayed')
def step(context):
    pass

浏览器.py

from selenium import webdriver

class Browser(object):

    base_url = 'http://www.test.com'
    driver = webdriver.Chrome("E:\Selenium Server\chromedriver.exe")
    driver.implicitly_wait(10)

    def close(self):
        """
        close the webdriver instance
        """
        self.driver.quit()

    def visit(self, location=''):
        """
        navigate webdriver to different pages
        """
        url = self.base_url + location
        self.driver.get(url)

    def find_by_id(self, selector):
        """
        find a page element in the DOM
        """
        return self.driver.find_element_by_id(selector)

环境.py

from browser import Browser
from selenium import webdriver

def before_all(context):
  context.browser = Browser()

def after_all(context):
  context.browser.close()

它没有找到 find_element,我想使用它以便可以在网页上找到元素。在 Behave 中使用 Fine_element 的正确语法是什么?

谢谢,里亚兹

4

1 回答 1

3

你不应该使用context.browser而不是context

search_field = context.browser.find_element(By.XPATH, 'id("twotabsearchtextbox")')

或者,如果find_element()方法未在Browser对象上公开,则进入内部驱动程序:

search_field = context.browser.driver.find_element(By.XPATH, 'id("twotabsearchtextbox")')

您也可以使用find_by_id()问题中提到的方法:

search_field = context.browser.find_by_id("twotabsearchtextbox")
于 2016-09-05T16:10:33.143 回答