TypeError:类型' NoneType'的对象迭代文件时没有len()

时间:2017-07-09 12:10:49

标签: python python-3.x

我试图迭代两个文本文件。我有两个文本文件,一个用于'用户名'另一个是密码'。

当我使用带有单个参数的函数时,它可以工作。 here's the code但不是在使用2个或多于1个参数时。

这是我的代码:

import os
import time
import getpass
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


def Quora_bot():

    username = None
    password = None

    chromedriver  = 'F:\All  Folders\chromedriver\shit.exe'

#Uncomment this block if you don't want images to load(makes the procss a little bit faster)
    '''
    chromeOptions = webdriver.ChromeOptions()
    prefs = {"profile.managed_default_content_settings.images":2}
    chromeOptions.add_experimental_option("prefs",prefs)
    browser = webdriver.Chrome(chromedriver, chrome_options=chromeOptions)
    '''

    browser = webdriver.Chrome(chromedriver)
    browser.get("http://www.quora.com")       # Quora home page
    time.sleep(10)

    # Logging into Quora
    form = browser.find_element_by_class_name('regular_login')
    email = form.find_element_by_name("email")
    password = form.find_element_by_name("password")
    button_element = browser.find_element_by_xpath("//*[@value='Login']")
    email.send_keys(username)
    password.send_keys(password)


with open('newfile.txt') as u:
      for line in u:
        rr = line


with open('password.txt') as p:
     for linee in p:
       pp = linee

Quora_bot()
username = rr
password = pp

我收到了这个错误。

    F:\Python_Installer\python.exe C:/Users/user/PycharmProjects/Quora_Bot/westing.py
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/Quora_Bot/westing.py", line 45, in <module>
    Quora_bot()
  File "C:/Users/user/PycharmProjects/Quora_Bot/westing.py", line 32, in Quora_bot
    email.send_keys(username)
  File "F:\Python_Installer\lib\site-packages\selenium\webdriver\remote\webelement.py", line 348, in send_keys
    {'text': "".join(keys_to_typing(value)),
  File "F:\Python_Installer\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

1 个答案:

答案 0 :(得分:0)

因此,我们最终不会在评论中包含所有内容,让我为您总结一下

  1. 不要覆盖inbuild关键字,例如forobject

  2. 您的for_game()' function which is (i guess) should reset字母and动物`没有重置全局变量,只在本地重置。因此它没有意义,因为它最终没有做任何事情。

  3. 假设您在我们看不到的地方定义了val,请确保将其定义为length属性的对象,如string或{{1 (我猜大多数/所有的迭代都有。目前它似乎是list所以要么你把它定义为None,要么把它设置为等于没有{{1}的函数声明。

  4. 我不认为你的None语句正如你所希望的那样工作,因为你永远不会return文件的内容而只保留对象。您需要使用with

  5. 阅读

    但是如果没有必要的代码,我们就再也无法帮到你了。