简体   繁体   中英

Python object of type long has no Len() error?

so i have this python code written which basically pulls data from an excel sheet using openpyxl and then fills an online web form using selenium. I managed to get the code working when it just pulls data from column B in the sheet but when I add the other columns for it to pull the data from i get this error:

Traceback (most recent call last):
File "/Users/John/Desktop/run3.py", line 103, in <module>
main()
File "/Users/John/Desktop/run3.py", line 76, in main
first.send_keys(mrn)
File "/Library/Python/2.7/site-packages/selenium-3.13.0- 
py2.7.egg/selenium/webdriver/remote/webelement.py", line 478, in send_keys
{'text': "".join(keys_to_typing(value)),
File "/Library/Python/2.7/site-packages/selenium-3.13.0- 
py2.7.egg/selenium/webdriver/common/utils.py", line 150, in keys_to_typing
for i in range(len(val)):
TypeError: object of type 'long' has no len()

I have no idea where im going wrong or even what is producing this error. Here is the code that I have so far:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from tqdm import tqdm
import openpyxl
import datetime
import Tkinter
import tkFileDialog
import time
from time import sleep
import re
import sys


#file selector*
Tkinter.Tk().withdraw()
file = tkFileDialog.askopenfilename()
path = str(file)

wk = openpyxl.load_workbook(path)
print("Active Sheet: "+ wk.active.title)
sh = wk['Sheet1']

#data select
ro = raw_input("What is First Cell: ")
m = re.sub("\D", "", ro)
m2 = int(m)

#Total rows and columns
rows = sh.max_row - m2 + 1
columns = sh.max_column
print("Total Rows: " + str(rows))
print("Total Columns: " + str(columns))


driver = webdriver.Chrome()
driver.get("http://www.clickdimensions.com/form/default.html")
driver.maximize_window()

k2 = -1

def main():
    global k2
    global rows
    k2 = k2 + 1
    for column in tqdm("B"):
        global k2
        rowi = int(m) + int(k2)
        rowk = str(rowi)
        cell_name = "{}{}".format(column, rowk)
        cell_name2 = "{}{}".format("D", rowk)
        cell_name3 = "{}{}".format("E", rowk)
        cell_name4 = "{}{}".format("H", rowk)
        mrn = sh[cell_name].value
        mrn1 = sh[cell_name2].value
        mrn2 = sh[cell_name3].value
        mrn3 = sh[cell_name4].value

    print("Working on Entry: " + str(k2))
    sh['L' + rowk].value = "Complete"

    try:
        if sh[cell_name].value is None:
            print("Accessioning Completed")
            raw_input("Press Enter to Exit")
            da = datetime.datetime.now().strftime("%d-%m-%Y %H/%M")
            #NEED TO CHANGE THE FILE SAVE LOCATION
            wk.save("/Users/arsham/Desktop/IMPACT " + da + "Complete" + 
".xlsx")
            raise sys.exit()
    except:
        raise sys.exit()

    first = driver.find_element_by_name('txtFirstName')
    first.send_keys(mrn)
    last = driver.find_element_by_name('txtLastName')
    last.send_keys(mrn1)
    email = driver.find_element_by_name('txtFormEmail')
    email.send_keys("test@gmail.com")
    cmp = driver.find_element_by_name('txtCompany')
    cmp.send_keys(mrn2)
    cmp = driver.find_element_by_name('chkNewsletter')
    cmp.click()
    cnt = driver.find_element_by_name('optCountry')
    cnt.send_keys("Bahrain")

    driver.implicitly_wait(30)

    addbutton = 
driver.find_element_by_xpath("/html/body/form/table/tbody/tr[12]/td[2]/input")
   addbutton.click()

   try:
       html_source = driver.page_source
       if "Oops" in html_source:
           driver.get("http://www.clickdimensions.com/form/default.html")
           main()
   except NoSuchElementException:
       print("Waiting for Completion")
       WebDriverWait(driver, 10).until(
           "Oops" in html_source
    )
main()

exit()

Apply str function upon passing value to the send_keys function if it's a number . Generally speaking send_keys accept string, but you provide number, that leads to the exception. Changing lines below will fix issue:

from

first.send_keys(mrn)

to

first.send_keys(str(mrn))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM