2016-09-05 56 views
0

我有一個示例BDD Python行爲代碼。當我運行的行爲test.feature網頁打開,但隨後我得到以下錯誤:Python行爲'上下文'對象沒有屬性'find_element'

'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的正確語法是什麼?

感謝,里亞茲

回答

2

你不是應該使用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") 
+0

:爲什麼上下文在每個函數中傳遞?硒有必要嗎?內容包含哪些內容? –

相關問題