webdriver実装126メールボックスで添付ファイル、タイトル、本文付きメールを送信


from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time, traceback

driver = None
wait = None

#    
def login_126():

    global driver
    global wait
    url = "http://www.126.com"
    driver = webdriver.Chrome(executable_path="d:\\chromedriver")
    driver.get(url)
    #         
    wait = WebDriverWait(driver, 10, 0.2)
    try:
        #         iframe
        wait.until(EC.frame_to_be_available_and_switch_to_it(( \
            By.XPATH, '//iframe[contains(@id,"x-URS-iframe")]')))
        time.sleep(3)
        # driver.switch_to.frame(driver.find_element_by_xpath(\
        # '//iframe[contains(@id,"x-URS-iframe")]'))

        #                       
        username = wait.until(lambda x: x.find_element_by_xpath( \
            '//input[@placeholder="        "]'))

        username.send_keys("xxxx")

        #          
        passwd = driver.find_element_by_xpath('//input[@name="password"]')
        passwd.send_keys("xxxxx")

        #          
        driver.find_element(By.ID, "dologin").click()

        #     
        driver.switch_to.default_content()

        #          “  ”        
        if wait.until(EC.visibility_of_element_located(('xpath', '//a[.="  "]'))):
            print("    !")
        else:
            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())

#              
def close_driver():
    global driver
    driver.quit()

def send_mail():
    global driver
    global wait
    try:
        #                      
        write_button = wait.until(EC.element_to_be_clickable(('xpath','//span[.="   "]')))
        write_button.click()

        #            
        receiver = wait.until(lambda x : x.find_element_by_xpath(\
                 '//input[@class="nui-editableAddr-ipt"]'))
        #receiver = wait.until(EC.visibility_of_element_located(('xpath','//span[.="   "]')))
        receiver.send_keys("[email protected]")

        #             
        subject = driver.find_element_by_xpath('//input[contains(@id,"subjectInput")]')
        subject.send_keys("2019     ?")

        #          ( send_keys()     input  )
        attachment = driver.find_element_by_xpath('//input[@class="O0"]')

        attachment.send_keys("e:\\python\\111.txt")

        #        
        wait.until(EC.visibility_of_element_located(('xpath','//span[.="    "]')))
        print("      !")

        time.sleep(3)

        #    frame 
        wait.until(EC.frame_to_be_available_and_switch_to_it(\
             ('xpath','//iframe[@class="APP-editor-iframe"]')))

        contentBox = driver.find_element_by_xpath('/html/body')
        contentBox.send_keys("                ,      ")

        #  frame,    
        driver.switch_to.default_content()

        #  
        driver.find_element_by_xpath(\
            '//footer[@class="jp0"]//span[text()="  "]//preceding-sibling::span//b').click()

        #         //h1[contains(@id,"succInfo")]    //h1[text()="    "]
        if wait.until(EC.visibility_of_element_located(('xpath','//h1[text()="    "]'))):
            print("      !")
        else:
            print("      !")

        time.sleep(5)

    except NoSuchElementException as e:
        print(traceback.print_exc())
    except TimeoutException as e:
        print(traceback.print_exc())
    except Exception as e:
        print(traceback.print_exc())

def main():
    login_126()  
    send_mail()
    close_driver()

if __name__ == "__main__":
    main()