TypeError:readData()缺少1个必需的位置参数:“ data”

时间:2019-11-12 12:01:45

标签: python python-3.x selenium-webdriver openpyxl

面临代码错误。我在python的学习阶段, 尝试使用硒为Python创建DDT框架。

userN = getData.readData(path,'Sheet1',r,1) TypeError: readData() missing 1 required positional argument: 'data'

下面的XLutil.py代码

import openpyxl

class getData():


def getRowCount(file,sheetName):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return (sheet.max_row)

def getColumnCount(file,sheetName):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return (sheet.max_column)

def readData(file,sheetName,rownum,columnno):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return sheet.cell(row=rownum,column=columnno).value

def writeData(file,sheetName,rownum,columnno,data):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    sheet.cell(row=rownum,column=columnno).value = data
    workbook.save(file)

下面的login.py代码

        path = 'c:/Users/mahmood/PycharmProjects/Selenium_automaton/Login.xlsx'
    # global path
    driver = self.driver
    row = getData.getRowCount(path,'Sheet1')
    for r in range(2,row+1):
      driver.get("https://xxx-eee.xyxxxve.com/")
      userN = getData.readData(path,'Sheet1',r,1)
      passW = getData.readData(path,'Sheet1',r,2)

      login = LoginPage(driver)
      login.enter_username(userN)
      login.enter_password(passW)
      login.click_login()

      homepage = HomePage(driver)
      homepage.click_welcome()
      homepage.click_logout()

我试图通过openpyxl从Excel工作表的用户名和密码中获取数据,但出现以下错误。

Testing started at 6:47 AM ...
C:\Users\mahmood\PycharmProjects\Selenium_automaton\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.2.3\helpers\pycharm\_jb_unittest_runner.py" --target login.LoginTest
Launching unittests with arguments python -m unittest login.LoginTest in C:\Users\mahmood\PycharmProjects\Selenium\SampleProjects\POMProjectDemo\Tests

C:\Users\mahmood\PycharmProjects\Selenium\SampleProjects\POMProjectDemo\Utility\XLUtil.py:8: DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
  sheet = workbook.get_sheet_by_name(sheetName)

Error
Traceback (most recent call last):
  File "C:\Users\mahmood\AppData\Local\Programs\Python\Python37-32\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Users\mahmood\AppData\Local\Programs\Python\Python37-32\lib\unittest\case.py", line 628, in run
    testMethod()
  File 

> "C:\Users\mahmood\PycharmProjects\Selenium\SampleProjects\POMProjectDemo\Tests\login.py",
> line 26, in test_login_valid
>     userN = getData.readData(path,'Sheet1',r,1) TypeError: readData() missing 1 required positional argument: 'data'


Assertion failed


Ran 1 test in 6.994s

FAILED (errors=1)
text Completed

1 个答案:

答案 0 :(得分:1)

Python没有重载,并非没有pythonlangutil之类的第三方API。

def readData(file, sheetName, rownum, columnno, data)

正在隐藏

def readData(file, sheetName, rownum, columnno)

因此,当您尝试使用4个参数调用它时,会收到错误消息,因为readData需要5个参数。您可以改用默认值

def readData(file,sheetName,rownum,columnno, data=None):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    if data:
        sheet.cell(row=rownum,column=columnno).value = data
        workbook.save(file)
    else:
        return sheet.cell(row=rownum,column=columnno).value