登录搜狐邮箱并发送邮件的3中方法

登录搜狐邮箱并发送邮件的3中方法写自定义目录标题

  • 用于设置剪切板内容
  • 键盘按键映射字典
  • 键盘键按下
  • 键盘键抬起

登录搜狐邮箱并发送邮件的3中方法
(1)传统的通过send_keys
(2)显示等待------切换frame—使用火狐才行(注意时间的等待)
(3)通过切换tab键-----替换了切换frame

Way1
#encoding=utf-8
from selenium import webdriver
import unittest,time,traceback
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException,NoSuchElementException
from selenium.webdriver.common.by import By

class TestDemo(unittest.TestCase):
def setUp(self):
#self.driver = webdriver.Chrome(executable_path = “d:\chromedriver”)
self.driver = webdriver.Firefox(executable_path=“d:\geckodriver”)
def test_SohuMailSendEMail(self):
url = “http://mail.sohu.com”
# 访问搜狐邮箱登录页
self.driver.get(url)
time.sleep(3)

    try:
        userName=self.driver.find_element_by_xpath('//input[@placeholder="请输入您的邮箱"]')
        userName.clear()
        userName.send_keys("fosterwu")
        passWord = self.driver.find_element_by_xpath('//input[@placeholder="请输入您的密码"]')
        passWord.clear()
        passWord.send_keys("1111")
        login= self.driver.find_element_by_xpath(u'//input[@value="登 录"]')
        login.click()
        wait = WebDriverWait(self.driver, 10)
        # 显示等待,确定页面是否成功登录并跳转到登录成功后的首页
        wait.until(EC.element_to_be_clickable((By.XPATH,'//li[.="写邮件"]')))
        self.driver.find_element_by_xpath(u'//li[text()="写邮件"]').click()
        time.sleep(2)
        receiver = self.driver.find_element_by_xpath('//div[@arr="mail.to_render"]//input')
         # 输入收件人
        receiver.send_keys("[email protected]")
        subject = self.driver.find_element_by_xpath('//input[@ng-model="mail.subject"]')
        # 输入邮件标题
        subject.send_keys(u"一封测试邮件!")
        time.sleep(3)
        # 获取邮件正文编辑区域的iframe页面元素对象
        iframe = self.driver.find_element_by_xpath('//iframe[contains(@id, "ueditor")]')
        # 通过switch_to.frame()方法切换进入富文本框中
        self.driver.switch_to.frame(iframe)
        # 获取富文本框中编辑页面元素对象
        editBox = self.driver.find_element_by_xpath("/html/body")
        # 输入邮件正文
        editBox.send_keys(u"邮件的正文内容")
        # 从富文本框中切换出,回到默认页面
        self.driver.switch_to.default_content()
        # 找到页面上的“发送”按钮,并单击它
        self.driver.find_element_by_xpath('//span[.="发送"]').click()
        # 显示都等待含有关键字串“发送成功”的页面元素出现在页面中
        wait.until(EC.visibility_of_element_located((By.XPATH, '//span[.="发送成功"]')))
        print (u"邮件发送成功")
    except TimeoutException:
        print (u"显示等待页面元素超时")
    except NoSuchElementException:
        print (u"寻找的页面元素不存在", traceback.print_exc())
    except Exception:
        # 打印其他异常堆栈信息
        print (traceback.print_exc())

def tearDown(self):
    # 退出IE浏览器
    self.driver.quit()

if name == ‘main’:
unittest.main()

Way2`
#encoding=utf-8
from selenium import webdriver
import unittest, time, traceback
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.by import By

class TestDemo(unittest.TestCase):

def setUp(self):
    # 启动Firefox浏览器
    self.driver = webdriver.Firefox(executable_path="d:\\geckodriver")
    #self.driver = webdriver.Ie(executable_path = "d:\\IEDriverServer")
    #self.driver = webdriver.Chrome(executable_path = "d:\\chromedriver")
    
def test_SohuMailSendEMail(self):
    url = "http://mail.sohu.com"
    # 访问搜狐邮箱登录页
    self.driver.get(url)
    time.sleep(3)
    
    try:
        userName = self.driver.find_element_by_xpath\
            ('//input[@placeholder="请输入您的邮箱"]')
        userName.clear()
        userName.send_keys("fosterwu")
        passWord = self.driver.find_element_by_xpath\
            ('//input[@placeholder="请输入您的密码"]')
        passWord.clear()
        passWord.send_keys("1111")
        login = self.driver.find_element_by_xpath('//input[@value="登 录"]')
        login.click()

#这里是超时的影响
wait = WebDriverWait(self.driver, 10)
# 显示等待,确定页面是否成功登录并跳转到登录成功后的首页
wait.until(EC.element_to_be_clickable
((By.XPATH, ‘//li[text()=“写邮件”]’)))
self.driver.find_element_by_xpath(’//li[text()=“写邮件”]’).click()
time.sleep(2)
receiver = self.driver.find_element_by_xpath
(’//div[@arr=“mail.to_render”]//input’)
# 输入收件人
receiver.send_keys(“[email protected]”)
time.sleep(3)
subject = self.driver.find_element_by_xpath
(’//input[@ng-model=“mail.subject”]’)
# 输入邮件标题
subject.send_keys(u"一封测试邮件!")
time.sleep(3)
# 获取邮件正文编辑区域的iframe页面元素对象
iframe = self.driver.find_element_by_xpath
(’//iframe[contains(@id, “ueditor”)]’)
# 通过switch_to.frame()方法切换进入富文本框中
self.driver.switch_to.frame(iframe)
# 通过JavaScript代码向邮件正文编辑框中输入正文
self.driver.execute_script(“document.getElementsByTagName(‘body’)
[0].innerHTML=‘邮件的正文内容;’”)
# 从富文本框中切换出,回到默认页面
self.driver.switch_to.default_content()
# 找到页面上的“发送”按钮,并单击它
self.driver.find_element_by_xpath(’//span[.=“发送”]’).click()
# 显示都等待含有关键字串“发送成功”的页面元素出现在页面中
wait.until(EC.visibility_of_element_located
((By.XPATH, ‘//span[.=“发送成功”]’)))
print ( u"邮件发送成功")
except TimeoutException:
print ( u"显示等待页面元素超时")
except NoSuchElementException:
print ( u"寻找的页面元素不存在", traceback.print_exc())
except Exception:
# 打印其他异常堆栈信息
print ( traceback.print_exc())

def tearDown(self):
    # 退出IE浏览器
    self.driver.quit()

if name == ‘main’:
unittest.main()

避免等待超时时,则需要设置对应的时间

(3)通过切换tab键–跳过切换frame
#encoding=utf-8
from selenium import webdriver
import unittest, time, traceback
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import win32clipboard as w
import win32api, win32con

用于设置剪切板内容

def setText(aString):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, aString)
w.CloseClipboard()

键盘按键映射字典

VK_CODE = {‘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 TestDemo(unittest.TestCase):

def setUp(self):
    # 启动Firefox浏览器
    self.driver = webdriver.Firefox(executable_path="d:\\geckodriver")

def test_SohuMailSendEMail(self):
    url = "http://mail.sohu.com"
    # 访问搜狐邮箱登录页
    self.driver.get(url)
    time.sleep(3)
    try:
        userName = self.driver.find_element_by_xpath\
            ('//input[@placeholder="请输入您的邮箱"]')
        userName.clear()
        userName.send_keys("fosterwu")
        passWord = self.driver.find_element_by_xpath\
            ('//input[@placeholder="请输入您的密码"]')
        passWord.clear()
        passWord.send_keys("1111")
        login = self.driver.find_element_by_xpath('//input[@value="登 录"]')
        login.click()
        wait = WebDriverWait(self.driver, 10)
        # 显示等待,确定页面是否成功登录并跳转到登录成功后的首页
        wait.until(EC.element_to_be_clickable\
                       ((By.XPATH, '//li[text()="写邮件"]')))
        self.driver.find_element_by_xpath('//li[text()="写邮件"]').click()
        time.sleep(2)
        receiver = self.driver.find_element_by_xpath\
            ('//div[@arr="mail.to_render"]//input')
        # 输入收件人
        receiver.send_keys("[email protected]")
        time.sleep(2)
        subject = self.driver.find_element_by_xpath\
            ('//input[@ng-model="mail.subject"]')
        # 输入邮件标题
        subject.send_keys(u"一封测试邮件!")
        # 输入完邮件标题后,按下Tab键可以将页面焦点切换到富文本框编辑区域
        subject.send_keys(Keys.TAB)
        subject.send_keys(Keys.TAB)
        time.sleep(2)
        # 设置剪切板内容,也就是将要输入的正文内容
        setText(u"邮件正文内容")
        # 模拟键盘的Ctrl + v组合键,将剪切板内容粘贴到富文本编辑区中
        keyDown("ctrl")
        keyDown("v")
        keyUp("v")
        keyUp("ctrl")
        # 找到页面上的“发送”按钮,并单击它
        self.driver.find_element_by_xpath('//span[.="发送"]').click()
        # 显示都等待含有关键字串“发送成功”的页面元素出现在页面中
        wait.until(EC.visibility_of_element_located\
                       ((By.XPATH, '//span[.="发送成功"]')))
        print (u"邮件发送成功")
    except TimeoutException:
        print (u"显示等待页面元素超时")
    except NoSuchElementException:
        print (u"寻找的页面元素不存在", traceback.print_exc())
    except Exception:
        # 打印其他异常堆栈信息
        print (traceback.print_exc())

def tearDown(self):
    # 退出IE浏览器
    self.driver.quit()

if name == ‘main’:
unittest.main()

你可能感兴趣的:(selenium)