简体   繁体   English

在 python 中逐行读取文件并在 for 循环 python 中使用

[英]Reading a file line by line in python and using in for loop python

Problem: When I run this program with selenium webdriver using "f.readline" the program prints the first password from my password list.问题:当我使用“f.readline”使用 selenium webdriver 运行此程序时,程序会打印我的密码列表中的第一个密码。 Once it loops it repeats the first password again.一旦它循环它再次重复第一个密码。 In contrast, when I attempt "f.readlines"a password does not transfer to the website.相反,当我尝试“f.readlines”时,密码不会传输到网站。 Goal: I am trying to get the program to attempt the next password every time the program loops.目标:我试图让程序在每次程序循环时尝试下一个密码。 My password file contains only one password per line.我的密码文件每行只包含一个密码。 Help is appreciated.帮助表示赞赏。

 with open('rockyou.txt', 'r') as f:
    passwords = f.readlines()
 for line in passwords:
    password = driver.find_element_by_xpath('//*[@id="rcmloginpwd"]')
    password.send_keys(passwords)

You are passing in the whole object each loop.您将在每个循环中传递整个 object。 Your last line just needs adjusting I think我认为你的最后一行只需要调整

 with open('rockyou.txt', 'r') as f:
    passwords = f.readlines()
 for line in passwords:
    password = driver.find_element_by_xpath('//*[@id="rcmloginpwd"]')
    password.send_keys(line)

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

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