TA的每日心情 | 无聊 2018-5-10 09:16 |
---|
签到天数: 172 天 连续签到: 2 天 [LV.7]测试师长
|
【分享】python+selenium 登录代码
1、导入excel中3组用户名和密码数据
2、for循环跑3次登录登出
现在想把3组(时间 用户名 密码 登录成功/失败)数据整行写入、整列写入excel?不知怎么做?应该放置代码那块合适?
#!/usr/bin/env python
#_*_coding:utf-8_*_
from selenium import webdriver
import time,xlrd,xlwt
from selenium.webdriver.common.action_chains import ActionChains
def OpenUrl():#启动访问域名
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('http://aibbtest.******.tv')
return driver
def UserData():#导入excel数据
book=xlrd.open_workbook('E:\\test.xlsx')
sheet=book.sheet_by_index(0)
col_username=sheet.col_values(0)
col_password=sheet.col_values(1)
login_data=dict(zip(col_username,col_password))
return login_data
def login(dribver,username,password):
driver.find_element_by_link_text('登录').click()
driver.switch_to_frame(driver.find_element_by_xpath('//iframe[@id="layui-layer-iframe1"]'))
time.sleep(2)
driver.find_element_by_xpath('//input[@placeholder="手机号/账号/邮箱"]').send_keys(username)
driver.find_element_by_xpath('//input[@placeholder="请输入密码"]').send_keys(password)
driver.find_element_by_xpath('//input[@value="登录"]').click()
driver.switch_to_default_content()
time.sleep(2)
center=driver.find_element_by_xpath('//div[@class="user-info hasLogin"]/div[1]/span')
time.sleep(2)
ActionChains(driver).move_to_element(center).perform()
#book =xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet('test_sheet',cell_overwrite_ok=True)
#i=0
try:
driver.find_element_by_xpath('//a[@class="loginOut"]').click()
print('登录成功')
#sheet.write(0,i+1,'登录成功')
except:
print('未登录成功')
#sheet.write(0,i+1,'登录失败')
time.sleep(2)
try:
driver.find_element_by_link_text('登录').click()
print('退出账号成功')
except:
print('退出账号失败')
#book.save("E:\\test1.xls")
time.sleep(2)
driver.close()
user=UserData()
for username,password in user.items():
if type(username)!=str:
username=int(username)
if type(password)!=str:
password=int(password)
driver=OpenUrl()
login(driver,username,password)
|
|