真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

自動(dòng)登錄火狐郵箱發(fā)送攜帶附件的郵件

# 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

#設(shè)置剪貼板內(nèi)容
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對(duì)象
        wait = WebDriverWait(self.driver, 10, 0.2)

        try:
            # 顯式等待用戶名輸入框可見,并獲取輸入框元素對(duì)象
            #username = wait.until( \
                #lambda x: x.find_element_by_xpath( \
                    #'//input[@placeholder="請(qǐng)輸入您的郵箱"]'))

            username = self.driver.find_element_by_xpath(\
                 '//input[@placeholder="請(qǐng)輸入您的郵箱"]')
            wait.until(EC.visibility_of(username))

            # username = wait.until(EC.visibility_of_element_located(\
            # ("xpath",'//input[@placeholder="請(qǐng)輸入您的郵箱"]')))

            username.clear()
            username.send_keys("xxxx@sohu.com")

            # 顯式等待密碼輸入框可見,并獲取輸入框元素對(duì)象
            passwd = wait.until(lambda x: x.find_element( \
                By.XPATH, '//input[@placeholder="請(qǐng)輸入您的密碼"]'))
            passwd.send_keys("xxxxxx")

            # 獲取登錄按鈕元素對(duì)象
            login_button = self.driver.find_element_by_xpath('//input[@value="登 錄"]')
            login_button.click()

            # 顯式等待“寫郵件”按鈕出現(xiàn),并獲取“寫郵件”按鈕元素對(duì)象(頁面有跳轉(zhuǎn))
            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()

            # 顯式等待收件人輸入框可見,并獲取輸入框元素對(duì)象(頁面有跳轉(zhuǎn))
            #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)
            # 直接獲取主題輸入框元素對(duì)象(可不用顯式等待,此時(shí)頁面已加載)
            subject = self.driver.find_element_by_xpath('//input[@ng-model="mail.subject"]')
            subject.send_keys("測(cè)試搜狐郵箱發(fā)郵件")

            # 獲取上傳附件元素對(duì)象
            attachment = self.driver.find_element_by_xpath('//span[.="添加附件(50M)"]')
            attachment.click()
            #attachment.send_keys("e:\\1.gif")

            time.sleep(3)
            #設(shè)置剪貼板的內(nèi)容          
            setText("e:\\1.gif")

            #粘貼內(nèi)容到文件輸入框
            time.sleep(3)
            keyDown("ctrl")
            keyDown("v")

            keyUp("v")
            keyUp("ctrl")

            time.sleep(2)#防止執(zhí)行太快沒有粘貼上
            #點(diǎn)擊彈出框的“打開”按鈕
            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()

            # 點(diǎn)擊發(fā)送按鈕
            self.driver.find_element_by_xpath('//span[.="發(fā)送"]').click()

            # 顯式等待"發(fā)送成功" 出現(xiàn)
            wait.until(EC.visibility_of_element_located(('xpath', '//span[.="發(fā)送成功"]')))
            print("郵件發(fā)送成功!")
        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()

分享標(biāo)題:自動(dòng)登錄火狐郵箱發(fā)送攜帶附件的郵件
文章位置:http://weahome.cn/article/ipgpsj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部