Python 行为 'Context' 对象没有属性 'find_element'

Python Behave 'Context' object has no attribute 'find_element'

我有一个示例 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'

我的代码片段是:

test.feature:

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  |

steps.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

browser.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)

environment.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 的正确语法是什么?

谢谢,里亚兹

你不应该使用 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")