Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 TypeError: object of type 'NoneType' has no len() - what do it mean?
#1
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)
Quote
#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?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#3
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
Quote
#4
@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)
Quote
#5
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  TypeError: 'tuple' object is not callable Chuck_Norwich 2 215 Feb-29-2020, 08:23 PM
Last Post: Chuck_Norwich
  AttributeError: type object 'FunctionNode' has no attribute '_TestValidateFuncLabel__ binhduonggttn 0 130 Feb-19-2020, 11:29 AM
Last Post: binhduonggttn
  TypeError: size; expecting a recognized type filling string dict a11_m11 0 174 Feb-10-2020, 08:26 AM
Last Post: a11_m11
  TypeError: type str doesn't define __round__ method emmapaw24 7 476 Feb-03-2020, 08:38 PM
Last Post: snippsat
  TypeError: can't multiply sequence by non-int of type 'str' emmapaw24 2 145 Feb-03-2020, 05:50 PM
Last Post: michael1789
  Type hinting - return type based on parameter micseydel 2 148 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Object of type set is not JSON serializable enigma619 5 1,424 Dec-19-2019, 03:30 PM
Last Post: enigma619
  AttributeError: 'NoneType' object has no attribute 'label' : What's wrong ? Etienne17 1 467 Dec-13-2019, 06:57 PM
Last Post: ibreeden
  type of object as a string Skaperen 1 206 Dec-02-2019, 08:01 AM
Last Post: midarq
  TypeError: 'NoneType' object is not subscriptable Jmekubo 5 17,319 Nov-07-2019, 03:59 AM
Last Post: markfilan

Forum Jump:


Users browsing this thread: 1 Guest(s)