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



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

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:UsersbhDownloadschromedriver')
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

发生这种情况是因为您没有为 chaine[1] 赋值。 您需要在使用之前声明它。为什么使用睡眠功能? 打印链条[1]以查看其是否为空

相关内容

最新更新