学习python下使用selenium2自动测试第6天,参数化编程这节课花了两天时间。
本次编程主要时间是花在熟悉python上
知识点or坑点:
1、读取txt、xml、csv等文件存储的账号、密码
txt文件格式,逗号分割(也可使用其他符号):
www.126.com,user1,pwd1
www.qq.com,user2,pwd2
www.163.com,user3,pwd3
user_file = open('user_info.txt','r')
lines = user_file.readlines()
user_file.close() for line in lines:
mail = line.split(',')[0]
username = line.split(',')[1]
pwd = line.split(',')[2]
print(mail,username,pwd)
2、打开多窗口,定位新窗口
获取所有窗口句柄:cur_windows = dr.window_handles
定位窗口:
for cur_window in cur_windows:
dr.switch_to.window(cur_window)
3、python编程中self的作用
在我的理解中,self是全局的this对象,定义一个class LoginSetup():
self就是指LoginSetup这个对象本身
在本class中定义多个对象时,可使用self.function( [param1,param2,...] )来调用,
被调用方法的参数self为默认参数,真实接收参数从第二个开始
被调用函数:
def open_url(self,url):
js = 'window.open("'+url+'")'
print(js)
self.driver.execute_script(js)
调用函数:
def login(self):
json_lines = []
……
self.open_login(json_lines)
4、python的init初始化,是前后两个下划线横杠(坑点)
#初始化,两个下划横杠
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
url = 'http://www.baidu.com'
self.init_url = url
self.driver.get(url)
在类运行时,初始化一些参数
5、python的in
for 循环、比较部分字符串都可使用in
foreach:
for line in lines:
print(line['username'])
print(line['pwd'])
比较字符串:
if 'QQ' in url:
print('true')
登陆QQ邮箱代码:
from selenium import webdriver
from time import sleep class QqLogin(): def user_login(dr,username,pwd): print(username,pwd) dr.switch_to.frame("login_frame")
idInput = dr.find_element_by_id('u')
pwdInput = dr.find_element_by_id('p')
idInput.clear()
idInput.send_keys(username)
pwdInput.clear()
pwdInput.send_keys(pwd) #登录
dr.find_element_by_id('login_button').click() #返回上级frame
#dr.switch_to.parent_frame() #返回主frame,此处两个方法都可以
dr.switch_to.default_content() #调用返回主frame需要等一下
sleep(1) switchs = dr.find_elements_by_css_selector('div')
print( len(switchs) ) #获取登录用户名、邮箱
name = dr.find_element_by_id('useralias')
email = dr.find_element_by_id('useraddr')
print('qq登录成功|',name.text,'---',email.text) #dr.quit()
登陆网易邮箱代码
from time import sleep class WyLogin(): #登录
def user_login(driver,username,pwd):
sleep(1)
print( driver.current_url )
driver.switch_to.frame('x-URS-iframe')
emailInput = driver.find_element_by_name("email")
emailInput.clear()
#emailInput.send_keys(username)#火狐执行无效
email_id = emailInput.get_attribute("id")
js = 'document.getElementById("'+email_id+'").value="'+username+'"'
print(js)
driver.execute_script(js)#执行js
pwdInput = driver.find_element_by_name("password")
pwdInput.clear()
pwdInput.send_keys(pwd)
dologin = driver.find_element_by_id("dologin")
dologin.click() print('网易邮箱登陆成功') driver.switch_to.default_content()
登陆方法:
# coding=utf-8
from selenium import webdriver
from time import sleep
from loginQq import QqLogin
from loginWy import WyLogin class LoginSetup(): #初始化,两个下划横杠
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
url = 'http://www.baidu.com'
self.init_url = url
self.driver.get(url) #登录
def login(self):
user_file = open('user_info.txt','r')
lines = user_file.readlines()
user_file.close() try:
json_lines = [] for line in lines:
lineArr = line.split(',')
mail_type = lineArr[0]
mail = lineArr[1]
username = lineArr[2]
pwd = lineArr[3] # 打开浏览器窗口,定位当前窗口
url = 'http://'+mail
self.open_url(url) json_line = {}
json_line['username'] = username
json_line['pwd'] = pwd
json_line['mail'] = mail
json_line['mail_type'] = mail_type
json_lines.append(json_line)
#for end print(json_lines)
self.open_login(json_lines) #关闭浏览器
#self.driver.quit() except BaseException as error:
print('error:',error)
self.driver.quit()
#end login #打开新窗口
def open_url(self,url):
js = 'window.open("'+url+'")'
print(js)
self.driver.execute_script(js)
'''
win_handles = self.driver.window_handles
print( len(win_handles) )
for hand in win_handles:
print( hand ) cur_window = self.driver.current_window_handle
self.driver.switch_to.window(cur_window)
print('now url is ',self.driver.current_url)
'''
# win_handles = self.driver.window_handles
#end open_url #定位新打开窗口,登录
def open_login(self,json_lines):
dr = self.driver
cur_windows = dr.window_handles
print( len(cur_windows) )
username = ''
pwd = ''
mail_type = '' for cur_window in cur_windows:
dr.switch_to.window(cur_window)
cur_url = dr.current_url
print('cur_url 1 = ',cur_url) for line in json_lines:
mail = line['mail']
mail_in = mail.replace('www.','')
print(mail_in,cur_url)
if mail_in in cur_url:
print('username')
mail_type = line['mail_type']
username = line['username']
pwd = line['pwd'] print(mail_type,username) if username == '':
continue #调用登录方法
print('username is ',username)
if 'QQ' in mail_type:
QqLogin.user_login(dr,username,pwd)
if 'WY' in mail_type:
WyLogin.user_login(dr,username,pwd) # end open_login #调用登录方法
LoginSetup().login()
txt文件格式:
WY,www.126.com,user1,pwd1
WY,mail.163.com,user2,pwd2
QQ,mail.qq.com,user3,pwd3