自動登録火狐メールボックス添付ファイル付きメール送信


# 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("[email protected]")

            #            ,          
            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("[email protected]")

            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()