【发布时间】:2020-08-01 19:18:03
【问题描述】:
我有两个文件,test_e2e.py 和 CheckOutPage.py。 CheckOutPage.py 中有一个方法 'getProducts()',它返回具有特定 xpath 的所有元素的列表。此列表返回到 test_e2epage.py 中的变量“products”。现在,我正在遍历 'products' 列表并尝试应用存在 CheckOutPage.py 的方法 'getProductName()',但我不能这样做。代码如下。
CheckOutPage.py-
from selenium.webdriver.common.by import By
class CheckOutPage:
def __init__(self, driver): #Constructor
self.driver = driver
products = (By.XPATH, "//div[@class='card h-100']")
productName = (By.XPATH, "div/h4/a")
def getProducts(self):
return self.driver.find_elements(*CheckOutPage.products)
def getProductName(self):
return self.driver.find_element(*CheckOutPage.productName)
test_e2e.py-
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
# @pytest.mark.usefixtures("setup")
from pageObjects.CheckOutPage import CheckOutPage
from pageObjects.HomePage import HomePage
from utilities.BaseClass import BaseClass
class TestOne(BaseClass):
def test_e2e(self):
# Select only Blackberry.
checkOutPage = CheckOutPage(self.driver)
products = checkOutPage.getProducts()
for product in products:
#productName = product.find_element_by_xpath("div/h4/a").text
Name = product.checkOutPage.getProductName()
if Name == "Blackberry":
product.checkOutPage.selectProduct().click()
break
代码在 test_e2e.py 的第三行 for 循环中失败。 错误是“AttributeError: 'webElement' object has no attribute 'checkoutPage'”。请帮帮我。我被卡住了。
【问题讨论】:
标签: python selenium oop selenium-webdriver pageobjects