数据驱动方式登录126邮箱
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据驱动方式登录126邮箱相关的知识,希望对你有一定的参考价值。
数据文件:包含个用户及密码
xxxxxx,yyyyyy
xxxxx,yyyyy
#encoding=utf-8
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
with open("userinfo.txt",encoding="utf-8") as file_obj:
for line in file_obj:
user = line.strip().split(",")[0]
passwd = line.strip().split(",")[1]
url = "https://mail.126.com"
#打开浏览器
driver = webdriver.Chrome(executable_path = "e:\chromedriver")
#访问163邮箱
driver.get(url)
#暂停3秒,等待页面出现,这个地方要加sleep,不然切入frame会出错
time.sleep(3)
#定位登录所在的frame
frame = driver.find_element_by_id("x-URS-iframe")
#切换进入frame
driver.switch_to.frame(frame)
time.sleep(2)
#获取用户名、密码输入框元素对象
username = driver.find_element_by_xpath(‘//input[@name="email"]‘)
password = driver.find_element_by_xpath(‘//input[@name="password"]‘)
#显式等待用户名输入框出现,清空,并输入用户名
WebDriverWait(driver,10).until(EC.visibility_of(username))
username.clear()
username.send_keys(user)
#显式等待密码输入框出现,清空,并输入密码
WebDriverWait(driver,10).until(EC.visibility_of(password))
password.clear()
password.send_keys(passwd)
#显式等待定位到登录按钮并点击
WebDriverWait(driver,10).until(lambda x:x.find_element_by_id("dologin")).click()
time.sleep(5)
assert "退出" in driver.page_source
time.sleep(5)
driver.quit()
以上是关于数据驱动方式登录126邮箱的主要内容,如果未能解决你的问题,请参考以下文章
python2+selenium+mail,自动登录126邮箱
Mayi_Selenium登录126邮箱,定位不到账号输入框解决办法