将代码包装到 Python 中不同 class 的方法中

Wrapping the code into a method in a different class in Python

--主文件

    def test_e2e(self):
    # Home Page
    # Object created for HomePage class in home_page file
    home_page = HomePage(self.driver)

    # CheckOut Page
    # Object created for CheckOutPage class in home_page file
    checkout_page = home_page.shop_items()

    # get product names
    products = checkout_page.get_products()

    for product in products:
        product_name = product.find_element_by_css_selector("div h4").text

-- checkoutpagefile

class CheckOutPage:
products = (By.CSS_SELECTOR, "div[class='card h-100']")
product_text = (By.CSS_SELECTOR, "div h4")

def __init__(self, driver):
    self.driver = driver

#  products = self.driver.find_elements_by_css_selector("div[class='card h-100']")
# find_element_by_css_selector("div h4").text

def get_products(self):
    # get the products
    return self.driver.find_elements(*CheckOutPage.products)

def getproduct_text(self):
    # get product text
    pass

我想删除主文件中的这个“”.find_element_by_css_selector("div h4").text””部分,然后用一种方法包装到结帐页面class中,如何才能我做到了?

这可能不是最好的实现,但我会着手将以下方法添加到 CheckOutPage class。

# Product as an argument
def get_product_text(self, product):
    return product.find_element_by_css_selector("div h4").text

# Products as an argument
def get_products_text(self, products):
    return [product.find_element_by_css_selector("div h4").text for product in products]

# If the sole purpose of get_products method is to only get the products text
def get_products_text(self):
    products = self.get_products()
    return [product.find_element_by_css_selector("div h4").text for product in products]