emeraldgold07
emeraldgold07

Reputation: 303

Selenium Python AttributeError: 'NoneType' object has no attribute 'send_keys'

I'm new in Selenium WebDriver, and currently using Python to do scripting. Now I want to apply parameter, which I applied data driven method using Excel. Basically only the first loop ok, it can read and write data for first row, but after that cannot.

Firstly, I create this in Python file (as new module):

import openpyxl

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,columno):
    workbook = openpyxl.load_workbook(file)
    sheet = workbook.get_sheet_by_name(sheetName)
    return sheet.cell(row=rownum, column=columno).value

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

Next, I write this code to include data driven test in my login and logout process:

path="<<my excel path>>"

rows=XLUtils.getRowCount(path, 'Sheet1')

for r in range(2,rows+1):
    email=XLUtils.readData(path,"Sheet1",r,1)
    password=XLUtils.readData(path,"Sheet1",r,2)

    driver.implicitly_wait(100)
    time.sleep(3)
    driver.find_element_by_xpath('<<my email field xpath element>>').send_keys(email)
    driver.implicitly_wait(100)
    time.sleep(3)
    driver.find_element_by_xpath('<<my password field xpath element>>').send_keys(password)
    driver.implicitly_wait(100)
    time.sleep(3)
    driver.find_element_by_xpath('<<my click login xpath element').click()
    driver.implicitly_wait(100)
    time.sleep(3)
    
    if driver.title=="<<to verify page title>>":
        print("test passed")
        XLUtils.writeData(path,"Sheet1",r,3,"test passed")
    else:
        print("test failed")
        XLUtils.writeData(path, "Sheet1", r, 3, "test failed")

    # Logout
    driver.find_element_by_css_selector("<<my logout element>>").click()
    time.sleep(2)
    driver.find_element_by_css_selector("<<my logout element>>").click()
    time.sleep(3)

I got the error AttributeError: 'NoneType' object has no attribute 'send_keys', so I thought including waits/sleep could solve the problem, but I still don't find a way to fix it. Can anyone help? Appreciate it. Thanks.

Upvotes: 2

Views: 7811

Answers (1)

rahul rai
rahul rai

Reputation: 2326

Issue is with function return type. As seeing structure of your code I can assume you are creating your WebDriver driver in some set up class and passing it around.

Now you need to understand when you don't add a return type to a function in python, by default it returns None. As a result in setup method is self.driver set to None.

Please add return driver statement to your setup method and it should work.

Upvotes: 2

Related Questions