Python Selenium TypeError:缺少 1 个必需的位置参数:'self'

问题描述 投票:0回答:1

尝试运行我的代码时不断收到此错误。我是 Python Selenium 的新手,我无法理解这个错误: 类型错误:practicepagebutton() 缺少 1 个必需的位置参数:'self'

Test_e2e file:

from pageObjects.HomePage import HomePage
from pageObjects.LoginPage import LoginPage
from pageObjects.PracticePage import PracticePage
from utilities.BaseClass import BaseClass
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import pytest
import time


`class TestAutomation(BaseClass):

    def test_e2e(self):
        homepage = HomePage(self.driver)
        practicePage = HomePage.practicepagebutton()-------------> getting error on this line
        practicePage.getPageTitle()
        loginpage = PracticePage.testLoginPage()
        loginpage.loginUsername().send_keys("student")
        loginpage.loginPassword().send_keys("Password123")
        loginpage.loginSubmit().click()



HomePage.py:

from selenium.webdriver.common.by import By

from pageObjects.PracticePage import PracticePage


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

    practice = (By.XPATH, "//*[text()='Practice']")

    def practicepagebutton(self):
        self.driver.find_element(*HomePage.practice).click()
        practicePage = PracticePage(self.driver)
        return practicePage


PracticePage.py:

from selenium.webdriver.common.by import By

from pageObjects.LoginPage import LoginPage


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

    testLoginPageButton = (By.XPATH, "//*[text()='Test Login Page']")
    def getPageTitle(self):
        return self.driver.find_element(*PracticePage.testLoginPageButton).title()

    def testLoginPage(self):
        self.driver.find_element(*PracticePage.testLoginPageButton).click()
        loginpage = LoginPage(self.driver)
        return loginpage

LoginPage.py:
from selenium.webdriver.common.by import By

Error:

test_e2e.py::TestAutomation::test_e2e 

============================== 1 failed in 4.03s ==============================
FAILED                             [100%]
test_e2e.py:15 (TestAutomation.test_e2e)
self = <test_e2e.TestAutomation object at 0x000002593CA5DE20>

    def test_e2e(self):
        homepage = HomePage(self.driver)
>       practicePagee = HomePage.practicepagebutton()
E       TypeError: practicepagebutton() missing 1 required positional argument: 'self'

test_e2e.py:18: TypeError

Process finished with exit code 1

有人可以帮我解决这个错误吗?尝试了互联网上的解决方案,但似乎不起作用? 预先感谢!

python selenium-webdriver pytest typeerror
1个回答
0
投票

问题是您直接使用该类,而不是创建该类的实例

改为这样做:

hp = HomePage(self.driver)
practicePagee = hp.practicepagebutton()
© www.soinside.com 2019 - 2024. All rights reserved.