缺少Python / Selenium函数调用和必需参数

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

我定义了以下两个函数:

def setUp(self, language):
    #defines the webdriver
    self.browser = webdriver.Firefox()

    #calls the website
    self.browser.get("http://www.test.com")

    #defines the language to be used
    self.INTERFACE_LANGUAGE = language
    if(language == "de"):
        self.username = "[email protected]"
        self.pwd = "test"
    elif(language == "en"):
        self.username = "[email protected]"
        self.pwd = "test"
    return self.username, self.pwd

def login_user(self, username, pwd):
    """login the user"""
    #fill in the userdata
    self.u_Name = self.browser.find_element_by_id("form_user_id")
    self.u_Name.send_keys(username)
    self.u_Pwd = self.browser.find_element_by_id("form_user_pwd")
    self.u_Pwd.send_keys(pwd)

    #submit the form
    self.submit_Login = self.browser.find_element(By.XPATH,'//input[@name="send" and @value="Log in"]')
    self.submit_Login.click()

当我尝试像这样调用setUp:

lang = "de"
username, pwd = setUp(lang)
login_user(username, pwd)

它返回:

Traceback (most recent call last):
  File "c:\Users\user\Documents\test\test.py", line 40, in <module>
    username, pwd = setUp(lang)
TypeError: setUp() missing 1 required positional argument: 'language'
    login_user(username, pwd)

请有人帮我解决这个问题。我不明白为什么它说缺少一个论点。

在此先感谢您的帮助!

python python-3.x selenium
1个回答
0
投票

正如@furas所说,我只是在课堂上使用这些功能,但事实并非如此。

所以解决方案是从参数列表中删除self:

def setUp(language):
    #defines the webdriver
    global browser, INTERFACE_LANGUAGE
    browser = webdriver.Firefox()

    #calls the website
    browser.get("http://www.test.com")

    #defines the language to be used
    INTERFACE_LANGUAGE = language
    if(language == "de"):
        username = "test"
        pwd = "test"
    elif(language == "en"):
        username = "test"
        pwd = "test"
    return username, pwd

def login_user(username, pwd):
    """login the user to their account"""
    #fill in the userdata
    u_Name = browser.find_element_by_id("form_user_id")
    u_Name.send_keys(username)
    u_Pwd = browser.find_element_by_id("form_user_pwd")
    u_Pwd.send_keys(pwd)

    #submit the form
    submit_Login = browser.find_element(By.XPATH,'//input[@name="send" and @value="Log in"]')
    submit_Login.click()
© www.soinside.com 2019 - 2024. All rights reserved.