Python Selenium登录脚本

Python Selenium登录脚本,python,loops,selenium,iteration,login-script,Python,Loops,Selenium,Iteration,Login Script,我正在尝试编写一个脚本,该脚本将获取一个凭据文本文件(由“:”分隔)并登录到一个网站,并在登录尝试成功与否时显示,然后注销,以循环的形式出现,直到到达文本文件的结尾 除了从文本文件读取并插入用户名/密码字符串的部分(第13-18行)和整个循环之外,我的大部分代码都正常工作: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.common.exceptions i

我正在尝试编写一个脚本,该脚本将获取一个凭据文本文件(由“:”分隔)并登录到一个网站,并在登录尝试成功与否时显示,然后注销,以循环的形式出现,直到到达文本文件的结尾

除了从文本文件读取并插入用户名/密码字符串的部分(第13-18行)和整个循环之外,我的大部分代码都正常工作:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from itertools import cycle
import time
#importing various modules for added functionality



browser = webdriver.Firefox()
browser.get("http://www.example.com")
#creates browser object  
#b.o. makes GET request to specified site

username = browser.find_element_by_id("email")
password = browser.find_element_by_id("password")   
##looks for the html element "email" on above site
##looks for the html element "password" on above site



with open('creds.txt', 'r') as fp:
        credentials = [x.strip().split(':') for x in fp.readlines()]

for uname,pwd in cycle(credentials):

    username.send_keys(username from creds.txt file)
    password.send_keys(password from creds.txt file) 
    ##send username from file as email element value
    ##send password from file as password element value

    login_attempt = browser.find_element_by_id("signInBtn")
    login_attempt.click()
    ##looks for the html element "signInBtn" on above site
    ##simulates clicking of button

    time.sleep(2)

    title = browser.title
    print title
    #find the title of the html page and print it

    welcome = browser.find_element_by_id("specific_element")
    welcome_text = welcome.text.split()[1]
    read_welcome = welcome_text.encode('utf-8')
    #after successful login, look for element that is only present after successful login
    #Welcome, <name>; only want the name here
    #name was displaying in unicode for some reason, this converts the name as a normal string

    if 'specific_string' in title:
    #if string found in title variable
        print "Login Sucessful for %r " % read_welcome
        #"Login Successful for <name>"
        browser.get("http://www.link_to_sign_out")
        #simulate signing out


    elif 'specific_string' not in title:
    #if string not found in title variable
        print "Sign In failed.."  
        #"Sign In failed.."
        browser.get("http://www.link_to_sign_out")
        ##simulate signing out

    else:
        print 'Error..'
        #If neither condition is met "Error" is printed

    browser.close()
从selenium导入webdriver
从selenium.webdriver.common.keys导入密钥
从selenium.common.Exception导入NoTouchElementException
从itertools导入周期
导入时间
#导入各种模块以添加功能
browser=webdriver.Firefox()
browser.get(“http://www.example.com")
#创建浏览器对象
#b、 o.向指定站点发出GET请求
用户名=浏览器。通过id(“电子邮件”)查找元素
密码=浏览器。通过id(“密码”)查找元素
##在上面的站点上查找html元素“email”
##在上面的站点上查找html元素“password”
以open('creds.txt','r')作为fp:
凭据=[x.strip().split(':'),用于fp.readlines()中的x
对于uname,周期内的pwd(凭证):
用户名。发送密钥(creds.txt文件中的用户名)
密码。发送密钥(来自creds.txt文件的密码)
##将文件中的用户名作为电子邮件元素值发送
##将密码作为密码元素值从文件发送
登录尝试=浏览器。通过id(“登录”)查找元素
登录尝试。单击()
##在上面的站点上查找html元素“signInBtn”
##模拟单击按钮
时间。睡眠(2)
title=browser.title
印刷品标题
#找到html页面的标题并打印它
welcome=浏览器。按\u id(“特定\u元素”)查找\u元素
welcome\u text=welcome.text.split()[1]
read\u welcome=welcome\u text.encode('utf-8')
#成功登录后,查找仅在成功登录后出现的元素
#欢迎;我只想知道名字
#由于某种原因,名称以unicode显示,这会将名称转换为普通字符串
如果标题中有“特定字符串”:
#如果在title变量中找到字符串
打印“成功登录%r”%read\u欢迎
#“已成功登录”
browser.get(“http://www.link_to_sign_out")
#模拟注销
elif“特定字符串”不在标题中:
#如果在title变量中找不到字符串
打印“登录失败…”
#“登录失败…”
browser.get(“http://www.link_to_sign_out")
##模拟注销
其他:
打印“错误…”
#如果两个条件都不满足,则打印“错误”
browser.close()
我读过另外两个SO条目,它们提出了类似的问题,但答案似乎并不完全符合我的意图。


与询问这些问题的用户类似,我的脚本使用硬编码凭据。我们将非常感谢您的帮助

为什么从循环中排除登录尝试?您只需多次输入凭据,而不登录站点。我编辑了代码,以包括循环尝试以及for循环中的其余操作。这仍然只测试文本文件中的第一个uname:pwd。如何提交每个额外的uname:pwd组合?尝试将您的
for
循环包含到
with
block中谢谢!我按照你的建议做了,又多玩了一点,终于成功了:)