# encoding=utf-8
from selenium import webdriver
import unittest, time, traceback
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import win32clipboard as w
import win32con
import win32api
from selenium.webdriver.common.keys import Keys
#设置剪贴板内容
def setText(aString):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
w.CloseClipboard()
# 键盘按键映射字典
VK_CODE = {
'enter':0x0D,
'ctrl':0x11,
'v':0x56}
# 键盘键按下
def keyDown(keyName):
win32api.keybd_event(VK_CODE[keyName], 0, 0, 0)
# 键盘键抬起
def keyUp(keyName):
win32api.keybd_event(VK_CODE[keyName], 0, win32con.KEYEVENTF_KEYUP, 0)
class MyClass(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox(executable_path="d:\\geckodriver")
#self.driver = webdriver.Chrome(executable_path="d:\\chromedriver")
def test_multext(self):
url = "http://mail.sohu.com"
self.driver.get(url)
# 定义WebDriverWait对象
wait = WebDriverWait(self.driver, 10, 0.2)
try:
# 显式等待用户名输入框可见,并获取输入框元素对象
#username = wait.until( \
#lambda x: x.find_element_by_xpath( \
#'//input[@placeholder="请输入您的邮箱"]'))
username = self.driver.find_element_by_xpath(\
'//input[@placeholder="请输入您的邮箱"]')
wait.until(EC.visibility_of(username))
# username = wait.until(EC.visibility_of_element_located(\
# ("xpath",'//input[@placeholder="请输入您的邮箱"]')))
username.clear()
username.send_keys("xxxx@sohu.com")
# 显式等待密码输入框可见,并获取输入框元素对象
passwd = wait.until(lambda x: x.find_element( \
By.XPATH, '//input[@placeholder="请输入您的密码"]'))
passwd.send_keys("xxxxxx")
# 获取登录按钮元素对象
login_button = self.driver.find_element_by_xpath('//input[@value="登 录"]')
login_button.click()
# 显式等待“写邮件”按钮出现,并获取“写邮件”按钮元素对象(页面有跳转)
writeMail_b = wait.until(EC.element_to_be_clickable( \
(By.XPATH, '//li[text()="写邮件"]')))
# writeMail_b = wait.until(\
# lambda x : x.find_element_by_xpath('//li[text()="写邮件"]'))
writeMail_b.click()
# 显式等待收件人输入框可见,并获取输入框元素对象(页面有跳转)
#receiver = wait.until(lambda x: x.find_element_by_xpath( \
#'//div[@arr="mail.to_render"]//input[@ng-model="addrInput"]'))
receiver = wait.until(EC.visibility_of_element_located(\
("xpath",'//div[@arr="mail.to_render"]//input[@ng-model="addrInput"]')))
receiver.send_keys("286542822@qq.com")
time.sleep(3)
# 直接获取主题输入框元素对象(可不用显式等待,此时页面已加载)
subject = self.driver.find_element_by_xpath('//input[@ng-model="mail.subject"]')
subject.send_keys("测试搜狐邮箱发邮件")
# 获取上传附件元素对象
attachment = self.driver.find_element_by_xpath('//span[.="添加附件(50M)"]')
attachment.click()
#attachment.send_keys("e:\\1.gif")
time.sleep(3)
#设置剪贴板的内容
setText("e:\\1.gif")
#粘贴内容到文件输入框
time.sleep(3)
keyDown("ctrl")
keyDown("v")
keyUp("v")
keyUp("ctrl")
time.sleep(2)#防止执行太快没有粘贴上
#点击弹出框的“打开”按钮
keyDown("enter")
keyUp("enter")
time.sleep(3)#等待上传完成
time.sleep(3)#切入frame前最好暂停下,否则可能定位不到frame
# 切入正文所在的frame
self.driver.switch_to.frame(self.driver.find_element_by_xpath( \
'//iframe[contains(@id,"ueditor")]'))
# 获取正文输入框
contentBox = self.driver.find_element_by_xpath('/html/body')
contentBox.send_keys("来自外太空的一封神秘邮件!")
# 从正文frame切出
self.driver.switch_to.default_content()
# 点击发送按钮
self.driver.find_element_by_xpath('//span[.="发送"]').click()
# 显式等待"发送成功" 出现
wait.until(EC.visibility_of_element_located(('xpath', '//span[.="发送成功"]')))
print("邮件发送成功!")
except NoSuchElementException as e:
print(traceback.print_exc())
except TimeoutException as e:
print(traceback.print_exc())
except Exception as e:
print(traceback.print_exc())
time.sleep(5)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()