将Selenium Web驱动程序中的两个字符串与Python中的行为驱动开发相匹配

时间:2017-05-17 19:39:43

标签: python selenium selenium-webdriver bdd gherkin

我正在编写一个测试,其中产品数量应与过滤数量匹配,但是当我运行代码时,我收到此错误:

'WebElement' object has no attribute '__getitem__'

我在这里分享我的BDD代码

@then('product count should match filter count')
def step_impl(context):
    product_count = WebDriverWait(context.browser, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="narrow-by-list"]/dd[7]/ol/li[8]/a'))
    )
    print(product_count.text)
    filter_count = context.browser.find_element_by_xpath('//*[@class="amount"]').text
    if(filter_count[8:-1] == product_count[6:-7]):
        print('product count matches with filter count')
    else:
        print('count not matches')

1 个答案:

答案 0 :(得分:0)

我已经在行为驱动开发中解决了我的问题。

filter_count = WebDriverWait(context.browser, 10).until(
    EC.presence_of_element_located((By.XPATH, '//dl[@id="narrow-by-list"]/dd[7]/ol/li[8]'))
)
print(filter_count.text)
product_count = context.browser.find_element_by_xpath('//*[@class="amount"]')
print(product_count.text)

if(filter_count.text[8:-1] == product_count.text[6:-8]):
    assert True
else:
    assert False