【问题标题】:"missing 1 required positional argument" when calling class method调用类方法时“缺少 1 个必需的位置参数”
【发布时间】:2021-04-29 16:31:08
【问题描述】:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

class Main():
    def Login(self,username,password):
        self.username = username
        self.password = password
        driver = webdriver.Chrome()
        driver.get("http://instagram.com")
        time.sleep(5)
        login_input = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')
        password_input = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input')
        login_input.send_keys(self.username)
        password_input.send_keys(self.password)
        

main = Main.Login("test","test")

问题是我收到此错误:TypeError: Login() missing 1 required positional argument: 'password'。 谁有解决方案?

【问题讨论】:

标签: python


【解决方案1】:

您的方法不是类方法,因此您无法使用Main.Login 访问它。将 @classmethod 装饰器添加到您的函数或首先创建 Main 实例并在该实例上调用您的方法。

另外,您的函数不会返回任何内容,因此将其分配给变量只会给您None

【讨论】:

    【解决方案2】:
    main = Main.Login("test","test")
    

    应该是:

    main = Main().Login("test","test")
    

    或:

    main = Main()
    main.Login("test","test")
    

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 2019-10-10
      • 2017-03-27
      相关资源
      最近更新 更多