登录163邮箱发邮件

#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

#打开浏览器
driver = webdriver.Chrome(executable_path = "e:\\chromedriver")

url = "https://mail.163.com"

#访问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("xxxxxx")

#显式等待密码输入框出现,清空,并输入密码
WebDriverWait(driver,10).until(EC.visibility_of(password))
password.clear()
password.send_keys("xxxxxxx")

#显式等待定位到登录按钮并点击
WebDriverWait(driver,10).until(lambda x:x.find_element_by_id("dologin")).click()

assert 
#发邮件
#暂停3秒,等待页面出现
time.sleep(3)

#定位到写信按钮,显式等待可点击后,并点击
writeButton = driver.find_element_by_xpath('//b[@class="nui-ico fn-bg ga0"]')

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//b[@class="nui-ico fn-bg ga0"]')))

writeButton.click()

#直接显示等待元素出现后,并点击
#WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath('//b[@class="nui-ico fn-bg ga0"]')).click()

#获取收件人输入框、主题输入框元素对象
send_address = driver.find_element_by_xpath('//input[@class="nui-editableAddr-ipt"]')
subject = driver.find_element_by_xpath('//input[@class="nui-ipt-input" and @tabindex="1"]')

#等待收件人输入框可用,并输入收件人
WebDriverWait(driver,10,0.2).until(EC.visibility_of(send_address))

send_address.send_keys("[email protected]")

#等待主题输入框可见,并输入主题
WebDriverWait(driver,10).until(EC.visibility_of(subject))

subject.send_keys(u"测试163发邮件")

#定位到附件按钮,并发送附件
attachment = driver.find_element_by_xpath('//input[@class="O0"]')

#用这个方法有问题
#WebDriverWait(driver,10).until(EC.visibility_of(attachment))

attachment.send_keys("e:\\1.jpg")

#WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath('//input[@class="O0"]')).send_keys("e:\\1.jpg")

#暂停几秒,等待附件上传
time.sleep(3)

#获取到正文所在的frame,并切入
iframe = driver.find_element_by_xpath('//iframe[@class="APP-editor-iframe"]')
driver.switch_to.frame(iframe)

#获取到正文输入框对象元素,并输入正文
content = driver.find_element_by_xpath('//body')
WebDriverWait(driver,10).until(EC.visibility_of(content))

content.send_keys(u"测试邮件发送")

#从正文frame切换出来,以便操作外面的发送按钮
driver.switch_to.default_content()

time.sleep(3)

#定位到发送按钮并点
sendButton = driver.find_element_by_xpath('//div[@class="frame-main-cont-body nui-scroll"]//div[@role="button"][1]//b')
sendButton.click()

#显式等待方式定位发送按钮,并点击
#WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath('//div[@class="frame-main-cont-body nui-scroll"]//div[@role="button"][1]//b')).click()

time.sleep(5)

#退出驱动并关闭所有窗口
driver.quit()

猜你喜欢

转载自blog.51cto.com/13496943/2170773