【分享】python+selenium 登录代码,同求测试结果如何写入excel?
【分享】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/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)
建议写个循环,每登录一次调用验证方法,并将输出excel方法放到验证方法里面即可
页:
[1]