Python(使用网络驱动程序通过user:password列表进行登录)

时间:2018-11-05 14:24:23

标签: python selenium login

我目前正在寻找基本登录网站的帮助,请先单击一个按钮,然后与下一个用户重新启动循环:从.txt文件中传递

目前我有atm:

from selenium import webdriver
from getpass import getpass

#define login
user = raw_input('Enter your username: ')
password = getpass('Enter your password : ')

#define what browser
driver = webdriver.Chrome('D:\Downloads\chromedriver')
driver.get('https://login.webzen.com/Home/Login?ReturnUrl=http%3A%2F%2Fwww.webzen.com%2Fevents%2Fhalloween-2018%2Ftrick-or-treat&Host=www.webzen.com')

#what boxes to use
username_box = driver.find_element_by_id('UserID')
username_box.send_keys(user)

password_box = driver.find_element_by_id('Password')
password_box.send_keys(password)

#press the actual fcking button
login_button = driver.find_element_by_id('submitButton')
login_button.submit()

#click candy
submit_button = driver.find_element_by_xpath('//*[@id="divLoginAfter"]/button')
submit_button.click()

我基本上想解决的问题是如何使用用户名:password制作一个名为account.txt的.txt文件。 要使其登录,执行脚本,然后登录其余帐户并执行相同的操作,将极大地帮助您!

1 个答案:

答案 0 :(得分:1)

我认为您的account.txt文件看起来像这样吗?

user1:password1
user2:password2
etc...

在这种情况下,您可以这样做:

from selenium import webdriver
from getpass import getpass

#define login
user = raw_input('Enter your username: ')
password = getpass('Enter your password : ')

#define what browser
driver = webdriver.Chrome('D:\Downloads\chromedriver')

with open('accounts.txt', 'r') as file:
    for line in file:
        user, password = line.split(':')

        driver.get('https://login.webzen.com/Home/Login?ReturnUrl=http%3A%2F%2Fwww.webzen.com%2Fevents%2Fhalloween-2018%2Ftrick-or-treat&Host=www.webzen.com')

        #what boxes to use
        username_box = driver.find_element_by_id('UserID')
        username_box.send_keys(user)

        password_box = driver.find_element_by_id('Password')
        password_box.send_keys(password)

        #press the actual fcking button
        login_button = driver.find_element_by_id('submitButton')
        login_button.submit()

        #click candy
        submit_button = driver.find_element_by_xpath('//*[@id="divLoginAfter"]/button')
        submit_button.click()