环境:windows7 python2.7 + selenium2 + firefox45
一、直接输入账号密码登录
# coding=utf-8 from selenium import webdriver import time url='https://mail.163.com/' loginname='+++++' #163邮箱账号 password='*****' #密码 browser = webdriver.Firefox() browser.get(url) browser.maximize_window() #窗口最大化 time.sleep(3) browser.switch_to.frame('x-URS-iframe') #切换到登录框 browser.find_element_by_name('email').clear() browser.find_element_by_name('email').send_keys(loginname) browser.find_element_by_name('password').clear() browser.find_element_by_name('password').send_keys(password) browser.find_element_by_id('dologin').click() time.sleep(3) browser.switch_to_default_content() #退出登录框 time.sleep(3) browser.switch_to.frame('x-URS-iframe') #进入确认登录框 browser.find_element_by_link_text("继续登录").click() time.sleep(20) browser.quit()
二、从文本读取账号密码
# coding=utf-8 import time from selenium import webdriver browser = webdriver.Firefox() url = "https://mail.163.com/" browser.get(url) time.sleep(1) browser.maximize_window() file = open("C:\Users\Administrator\Desktop\test.txt") #163邮箱账号密码存放在test.txt文件中 lines = file.readline() (username, password) = lines.strip(' ').split(',') #读取账号和密码 browser.switch_to.frame('x-URS-iframe') browser.find_element_by_name('email').send_keys(username) browser.find_element_by_name('password').send_keys(password) browser.find_element_by_id('dologin').click() file.close() time.sleep(3) browser.switch_to_default_content() time.sleep(3) browser.switch_to.frame('x-URS-iframe') browser.find_element_by_link_text("继续登录").click() time.sleep(20) browser.quit()