[英]how to apply a method to a variable stored in other file in python?
我有兩個文件, test_e2e.py和CheckOutPage.py 。 CheckOutPage.py中有一個方法“ getProducts() ”,它返回具有特定 xpath 的所有元素的列表。 此列表返回到test_e2epage.py中的變量“產品”。 現在,我正在遍歷“產品”列表並嘗試應用存在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' ”。 請幫幫我。 我被困住了。
您正在循環通過 CheckOutPage 返回的 web 元素,它們不是 CheckOutPage 返回的方法。 這就是我的寫法
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 = checkOutPage.getProductName(product)
if Name == "Blackberry":
product.checkOutPage.selectProduct().click()
break
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, product):
return self.driver.find_element(*CheckOutPage.productName)
Name = product.checkOutPage.getProductName()
循環中的這一行嘗試從product
object 訪問屬性checkOutPage
,該產品是 Selenium WebElement object 而不是CheckOutPage
ZA896668CFFDE69131BD 它沒有這樣的屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.