简体   繁体   English

列表索引超出范围错误(第二次)

[英]list index out of range error (second time)

This code works for the first line of the input file but fails in the second trial and I don't know why.此代码适用于输入文件的第一行,但在第二次试验中失败,我不知道为什么。 I tried to see if there are similar questions but I didn't found a solution.我试图看看是否有类似的问题,但我没有找到解决方案。

carac=":"
fichier = open("test", "r")
for i in range(2):
    time.sleep(0.5)

    chaine = fichier.readline().split(carac)
    print(chaine[0])

    driver = webdriver.Chrome(r'C:\Users\bh\Downloads\chromedriver')
    driver.get('https://www.twitter.com/login')
    username_box = driver.find_element_by_class_name('js-username-field')
    username_box.send_keys(chaine[0])
    password_box = driver.find_element_by_class_name('js-password-field')
    password_box.send_keys(chaine[1])

I have this error:我有这个错误:

Traceback (most recent call last):
  File "C:/Users/bh/PycharmProjects/test/twitter.py", line 199, in <module>
    password_box.send_keys(chaine[1])
IndexError: list index out of range

ps: the document is a .txt in the document there is something like this : ps:文档是.txt 文档中有这样的东西:

test@gmail.com:pass
test@gmail.com:pass

this happens because you havent assigned a value to chaine[1].发生这种情况是因为您还没有为 chaine[1] 赋值。 You need to declare it before you use it.您需要在使用之前声明它。 Why you use a sleep function?为什么要使用睡眠功能? print chaine[1] to see if its empty or not打印 chaine[1] 以查看其是否为空

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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