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

2024-09-29 00:14:18 发布

您现在位置:Python中文网/ 问答频道 /正文

这段代码适用于输入文件的第一行,但在第二次试用中失败了,我不知道为什么。我试图看看是否有类似的问题,但我没有找到解决办法。你知道吗

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

我有一个错误:

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:文档是一个.txt文件 文档中有这样的内容:

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

Tags: 文件intestcomboxsenddriverusername