You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium自动化测试OOP架构报错:TypeError参数需可迭代对象

Selenium OOP页面拆分报错:TypeError: find_element() argument after * must be an iterable, not function

环境信息

  • Python 3.11
  • Selenium 4.9.1
  • pytest 7.3.1
  • 编辑器:PyCharm

问题描述

我是自动化测试新手,正在学习Udemy课程《Selenium Webdriver with PYTHON from Scratch + Frameworks》的框架开发部分。采用OOP模式拆分页面代码时,出现报错:

TypeError: selenium.webdriver.remote.webdriver.WebDriver.find_element() argument after * must be an iterable, not function

报错出现在CheckoutPage.py第19行,但类似写法的第13、16行方法可正常运行。

相关代码

CheckoutPage.py

from selenium.webdriver.common.by import By


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

    cardTitles = (By.XPATH, "//div[@class='card h-100']/div/h4/a")
    cardFooter = (By.CSS_SELECTOR, ".card-footer button")
    checkoutButton = (By.CSS_SELECTOR, "nav-link btn btn-primary")

    def getcardTitles(self):
        return self.driver.find_elements(*CheckoutPage.cardTitles)

    def getcardFooter(self):
        return self.driver.find_elements(*CheckoutPage.cardFooter)

    def checkoutButton(self):
        return self.driver.find_element(*CheckoutPage.checkoutButton)

test_e2e.py

import time

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from pageObjects.CheckoutPage import CheckoutPage
from pageObjects.ConfirmPage import ConfirmPage
from pageObjects.HomePage import HomePage
from utilities.BaseClass import BaseClass


class TestOne(BaseClass):
    def test_e2e(self):
        homePage = HomePage(self.driver)
        homePage.shopItems().click()

        checkoutPage = CheckoutPage(self.driver)
        elements = checkoutPage.getcardTitles()
        confirmPage = ConfirmPage(self.driver)
        i = -1
        for ele in elements:
            i = i + 1
            if ele.text == "Blackberry":
                checkoutPage.getcardFooter()[i].click()
        # self.driver.find_element(By.CSS_SELECTOR, "a[class*='btn-primary']").click()
        checkoutPage.checkoutButton().click()
        time.sleep(5)
        # self.driver.find_element(By.CSS_SELECTOR, "button[class='btn btn-success']").click()
        confirmPage.checkoutConfirm().click()
        self.driver.find_element(By.ID, "country").send_keys("Bangla")
        wait = WebDriverWait(self.driver, 10)
        wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "div[class='suggestions'] ul li a")))
        self.driver.find_element(By.CSS_SELECTOR, "div[class='suggestions'] ul li a").click()
        self.driver.find_element(By.XPATH, "//label[@for='checkbox2']").click()
        self.driver.find_element(By.XPATH, "//input[@value='Purchase']").click()
        success = self.driver.find_element(By.XPATH, "//div[@class='alert alert-success alert-dismissible']").text
        assert "Success! Thank you!" in success

错误原因

  1. 命名冲突:CheckoutPage类中同时定义了名为checkoutButton的类定位器属性和同名方法。调用checkoutPage.checkoutButton()时,Python会将CheckoutPage.checkoutButton解析为方法对象而非定位器元组,解包方法对象时因它不是可迭代类型,触发报错。
  2. CSS选择器格式错误:checkoutButton的CSS选择器"nav-link btn btn-primary"缺少类选择器前缀.,导致Selenium无法正确定位元素。

解决方案

1. 重命名方法避免冲突

修改CheckoutPage.py中的方法名:

def getCheckoutButton(self):
    return self.driver.find_element(*CheckoutPage.checkoutButton)

同时更新test_e2e.py中的调用代码:

checkoutPage.getCheckoutButton().click()

2. 修正CSS选择器

调整checkoutButton定位器的选择器格式:

checkoutButton = (By.CSS_SELECTOR, ".nav-link.btn.btn-primary")

内容的提问来源于stack exchange,提问作者Fatin Istiaq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:52:12