python selenium 多账户自动登入163邮箱

pycharm一些快捷键:

‘ ctrl ’ +‘ / ’ :注释

‘ Tab ’ :同时缩进

‘ shift ’ +‘ Tab ’ :左移 一次缩进

本文webinfo.txt路径:C:\Python27\New Folder\case\webinfo.txt

userinfo.txt路径:C:\Python27\New Folder\case\userinfo.txt

webinfo内容:

url=https://mail.163.com/
account_name=email
pwd_name=password
login_id=dologin
logout=退出
relogin=relogin

userinfo内容:

uname=songping0914 pwd=qaz123456
uname=songpingzi66 pwd=songping66

usedate.py:

 1 #!/usr/bin/python
2 # -*- coding: UTF-8 -*-
3 # 这个程序 将webinfo 和 userinfo的内容读进来并转化为指定字典,列表
4 import codecs
5 def get_webinfo(path):
6 web_info = {}
7 config = codecs.open(path,'r','utf-8') #
8 # 有汉字的时候用
9 #config = open(path)
10 for line in config:
11 #result = line.split('=') 这样结尾会有 \n
12 result = [ele.strip() for ele in line.split('=')] #列表解析
13 web_info.update(dict([result])) #列表转化为字典 更新到webinfo中
14 return web_info
15
16 def get_userinfo(path):
17 user_info = []
18 config = open(path)
19 for line in config:
20 user_dict = {}
21 result = [ele.strip() for ele in line.split(' ')] #列表解析 ['uname=songping0914', 'pwd=qaz123456']
22 for r in result:
23 account = [ele.strip() for ele in r.split('=')] #列表解析 ['uname', 'songping0914'] ['pwd', 'qaz123456']
24 user_dict.update(dict([account])) # {'uname': 'songping0914', 'pwd': 'qaz123456'}
25 user_info.append(user_dict)
26 return user_info
27
28 if __name__ == '__main__':
29 webinfo = get_webinfo(r'C:\Python27\New Folder\case\webinfo.txt')
30 # for key in webinfo:
31 # print(key,webinfo[key])
32 userinfo = get_userinfo(r'C:\Python27\New Folder\case\userinfo.txt')
33 # for l in userinfo:
34 # print(l)
35 # print (userinfo)
36 print userinfo
37 print webinfo

autologin代码:

 1 #!/usr/bin/python
2 # -*- coding: UTF-8 -*-
3 from selenium import webdriver
4 from selenium.webdriver.support.ui import WebDriverWait
5 from time import sleep
6 from usedate import get_webinfo,get_userinfo
7 import codecs
8
9 #自动登入163邮箱
10 def openBrower():
11 webdriver_handle = webdriver.Chrome()
12 return webdriver_handle
13
14 def openUrl(handle,url):
15 '''load url'''
16 handle.get(url)
17 handle.maximize_window()
18 sleep(3)
19
20 def findElement(d,args): #定位登入页面输入框和登入按键的元素
21 '''1:login_name
22 2 account_name
23 3 pwd_name
24 4 login_id'''
25 # if mailid in args:
26 ele_account = WebDriverWait(d, 10).until(lambda d:d.find_element_by_name(args['account_name']))
27 ele_pwd = d.find_element_by_name(args['pwd_name'])
28 ele_login_btn = d.find_element_by_id(args['login_id'])
29 return ele_account,ele_pwd,ele_login_btn
30
31 def sendVals(eletuple,arg): # 将用户名密码输入到 以上元素位置
32 '''eletuple: 输入框 ,arg = each = userinfo_list {account :uname pwd}'''
33 listkey = ['uname','pwd']
34 i = 0
35 for key in listkey:
36 #eletuple[i].send_keys('')
37 eletuple[i].clear()
38 eletuple[i].send_keys(arg[key])
39 i+=1
40 eletuple[2].click()
41 sleep(2)
42
43 def logout(d,args):
44 d.find_element_by_link_text(args['logout']).click()
45 sleep(2)
46 d.find_element_by_class_name(args['relogin']).click()
47 sleep(2)
48
49 def get_ele_tmes(d,times,fun):
50 return WebDriverWait(d,times).until(func)
51
52 def login_test():
53 d = openBrower()
54 webinfo_dict = get_webinfo(r'C:\Python27\New Folder\case\webinfo.txt')
55 openUrl(d,webinfo_dict['url'])
56 d.switch_to.frame("x-URS-iframe")
57 ele_tuple =findElement(d,webinfo_dict)
58 userinfo_list = get_userinfo(r'C:\Python27\New Folder\case\userinfo.txt')
59 '''
60 webinfo_dict = {'url': 'https://mail.163.com/', 'login_id': 'dologin', 'account_name': 'email',
61 'pwd_name': 'password','logout':u'退出','relogin':'relogin'}
62
63 userinfo_list = [{'uname': 'songping0914', 'pwd': 'qaz123456'}, {'uname': 'songpingzi66', 'pwd': 'songping66'}, {'uname': 'songpingzi0914', 'pwd': 'songping66'}]
64
65 '''
66 #
67 for each in userinfo_list:
68 sendVals(ele_tuple,each)
69 logout(d,webinfo_dict)
70 sleep(3)
71 # openUrl(d, webinfo_dict['url'])
72 # d.switch_to.frame("x-URS-iframe")
73 # d.find_element_by_name("email").clear()
74 sleep(3)
75 d.quit()
76
77
78 if __name__ == '__main__':
79 login_test()

错误提示:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

最后,第一次登入后退出,第二次登入不能进行,还未修改成功,有知道的大神请指点,修改,不甚感激

PS 附上自动发邮件的脚本:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from selenium import webdriver
from time import sleep
url1='https://mail.163.com/'
url2='https://mail.126.com/'
account1='songping0914'
password1='qaz123456'
account2='songpingzi66'
password2='songping66'
driver = webdriver.Chrome()
driver.maximize_window() #最大化
driver.get(url1)
sleep(2) #切换到表单
driver.switch_to.frame("x-URS-iframe") #
driver.find_element_by_name("email").clear()
driver.find_element_by_name("email").send_keys(account1)
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys(password1)
driver.find_element_by_id("dologin").click()
sleep(2)
driver.switch_to.default_content()
driver.find_element_by_xpath(".//*[@id='_mail_component_70_70']").click()
sleep(2)
driver.find_element_by_class_name("nui-editableAddr-ipt").clear()
account3='songpingzi66@126.com'
driver.find_element_by_class_name("nui-editableAddr-ipt").send_keys(account3)
sleep(2)
driver.find_element_by_css_selector("div[id^='_mail_input_3']>input.nui-ipt-input").send_keys(u"测试webweb")
af = driver.find_element_by_class_name('APP-editor-iframe')
driver.switch_to.frame(af)
driver.find_element_by_xpath('//body[@class="nui-scroll"]').send_keys("Good study, day day up!")
driver.switch_to.default_content()
driver.find_element_by_css_selector("div[id^='_mail_button_2']>span.nui-btn-text").click()
上一篇:1.7 逆序数与归并排序[inversion pairs by merge sort]


下一篇:网络爬虫之requests模块的使用+Github自动登入认证