Python Forum

Full Version: TypeError: object of type 'NoneType' has no len() - what do it mean?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
note: i have another forum topic going with the same project. I've whited out a few lines to I can continue to practice until i resolve the other forum issue.
https://python-forum.io/Thread-Permissio...nied-error
In the mean time i have another issue which i'm raising here. Please be patient, i'm trying to learn and have a long long way to go with my development.

I have my code able to successfully look at a spreadsheet with a mix of working and not working username and passwords. The code will successfully add the username and password for the 4 different test cases but then gives the error message

TypeError: object of type 'NoneType' has no len

C:\Python37\python.exe C:/Users/David/PycharmProjects/POM/DataDrivenTestCase.py
test is passed
test failed
test failed
test failed
Traceback (most recent call last):
  File "C:/Users/David/PycharmProjects/POM/DataDrivenTestCase.py", line 24, in <module>
    driver.find_element_by_name("userName").send_keys(username)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 478, in send_keys
    {'text': "".join(keys_to_typing(value)),
  File "C:\Python37\lib\site-packages\selenium\webdriver\common\utils.py", line 150, in keys_to_typing
    for i in range(len(val)):
TypeError: object of type 'NoneType' has no len()
I'm guessing this is because of the loop I have? to it looks at the first 5 rows in my spreadsheet which have conditions and then hits the 6th row which is blank? Would my assumption be right?

If so what do i need to change?

I'm assuming i need to add some other condition to the following line
for r in range(2, rows):
    username = XLUtils.readData(path, "Sheet1", r, 1)
    password = XLUtils.readData(path, "Sheet1", r, 2)
Here is the code from the module of interest

from selenium import webdriver
import unittest
import XLUtils
 
profile_path = 'C:/FireFoxProfile'
profile = webdriver.FirefoxProfile(profile_path)
driver = webdriver.Firefox(firefox_profile=profile_path)
driver.implicitly_wait(5)
driver.get('http://www.demoaut.com/')
driver.maximize_window()
 
[b]path = "C://FireFoxProfile/login1.xlsx"[/b]
 
rows = XLUtils.getRowCount(path, 'Sheet1')
 
for r in range(2, rows):
    username = XLUtils.readData(path, "Sheet1", r, 1)
    password = XLUtils.readData(path, "Sheet1", r, 2)
 
    driver.find_element_by_name("userName").send_keys(username)
    driver.find_element_by_name("password").send_keys(password)
 
    driver.find_element_by_name("login").click()
 
    if driver.title == "Find a Flight: Mercury Tours:":
        print("test is passed")
        XLUtils.writeData(path, "Sheet1", r, 3, "test passed")
    else:
        print("test failed")
        XLUtils.writeData(path, "Sheet1", r, 3, "test failed")
 
    driver.find_element_by_link_text("Home").click()
 
    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
 
if __name__ == '__main__':
    unittest.main(verbosity=2)
well, it really, really, really has no length, so there is not much point to implementing the ability to get its length.

i think the line number is wrong or your code has been changed. what is line 20 supposed to do?
It searches the website for an element called "username" and then sends or populates that field with the username from my spreadsheet.

This part works. My spreadsheet has 5 rows. Row 2 has correct usernane and password. (if you look at the error message you will see it says this test is passed). Row 3,4 and 5 have incorrect username and password. You can see in the error message it says those tests fail.

So I think some error happens when it gets to row 6 which has no data in the spreadsheet
@Skaperen When you say it has no length..
We are talking about this line?
for r in range(2, rows):
So i need to be able to say do rows until there is no condition?

Here is my entire code - i've realigned the code so it points to line 20 where the error message. I've also added comments to make it easier to understand

Traceback (most recent call last):
  File "C:/Users/David/PycharmProjects/POM/DataDrivenTestCase.py", line 22, in <module>
    driver.find_element_by_name("userName").send_keys(username)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 478, in send_keys
    {'text': "".join(keys_to_typing(value)),
  File "C:\Python37\lib\site-packages\selenium\webdriver\common\utils.py", line 150, in keys_to_typing
    for i in range(len(val)):
TypeError: object of type 'NoneType' has no len()

Process finished with exit code 1
Module DataDrivenTestCase
from selenium import webdriver
import unittest
import XLUtils

profile_path = 'C:/FireFoxProfile'
profile = webdriver.FirefoxProfile(profile_path)
driver = webdriver.Firefox(firefox_profile=profile_path)
driver.implicitly_wait(5)
driver.get('http://www.demoaut.com/')
driver.maximize_window()

path = "C://FireFoxProfile/login1.xlsx"

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

# check rows in spreadsheet to obtains username and password and then
# insert username and password into  website
for r in range(2, rows+1):
    username = XLUtils.readData(path, "Sheet1", r, 1)
    password = XLUtils.readData(path, "Sheet1", r, 2)

    driver.find_element_by_name("userName").send_keys(username)
    driver.find_element_by_name("password").send_keys(password)

    driver.find_element_by_name("login").click()
#if login is successful then check website is on right page and pass/fail test
    if driver.title == "Find a Flight: Mercury Tours:":
        print("test is passed")
        XLUtils.writeData(path, "Sheet1", r, 3, "test passed")
    else:
        print("test failed")
        XLUtils.writeData(path, "Sheet1", r, 3, "test failed")
#return to homepage and insert password/username for next row in spreadsheet
    driver.find_element_by_link_text("Home").click()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

if __name__ == '__main__':
    unittest.main(verbosity=2)
This is the only other module i have - this module looks at the spreadsheet and determines the max row and column

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, 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)
ok guys - i figured it out. It wasn't an issue with my code. After spend way too much time on it, it turns out you need to have the spreadsheet closed in order to intereact python with the spreadsheet using openpyxl.

Apologies if i messed any of you around but I am grateful for your help. I'm learning and this is new to me.