【问题标题】:how to apply a method to a variable stored in other file in python?如何将方法应用于存储在python其他文件中的变量?
【发布时间】:2020-08-01 19:18:03
【问题描述】:

我有两个文件,test_e2e.pyCheckOutPage.pyCheckOutPage.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


    【解决方案1】:

    您正在循环通过 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)
    

    【讨论】:

      【解决方案2】:
      Name = product.checkOutPage.getProductName()
      

      循环中的这一行尝试从product 对象访问属性checkOutPage,该对象是Selenium WebElement 对象而不是CheckOutPage 对象。它没有这样的属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-10
        • 1970-01-01
        • 1970-01-01
        • 2021-01-24
        • 1970-01-01
        • 1970-01-01
        • 2018-10-25
        相关资源
        最近更新 更多