类型为long的Python对象没有Len()错误?

时间:2018-08-09 15:01:19

标签: python selenium openpyxl

所以我编写了这段python代码,基本上使用openpyxl从excel表中提取数据,然后使用selenium填写在线Web表单。当它只是从工作表中的B列中提取数据时,我设法使代码起作用,但是当我为其添加其他列以从中提取数据时,得到了此错误:

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()

我不知道即时通讯在哪里出错,甚至是什么导致了此错误。这是我到目前为止的代码:

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()

1 个答案:

答案 0 :(得分:2)

在将值传递给str函数(如果是数字)时应用send_keys函数。一般来说,send_keys接受字符串,但是您提供数字,这会导致异常。更改下面的行将解决问题:

来自

first.send_keys(mrn)

first.send_keys(str(mrn))