# 126郵箱,給自己發送已個郵件,包含附件、正文、標題的郵件 from selenium import webdriver import time driver = webdriver.Chrome(executable_path="e:\\chromedriver") # driver=webdriver.Ie(executable_path = "e:\\IEDriverServer") driver.get("https://mail.126.com") driver.maximize_window() driver.find_element_by_xpath("//a[text()='密碼登錄']").click() time.sleep(1) #切換用戶名、密碼登錄很重要 iframe = driver.find_element_by_xpath("//iframe[contains(@id,'x-URS-iframe')]") driver.switch_to_frame(iframe) username= driver.find_element_by_xpath("//input[@name='email']") username.clear() username.send_keys("zihailyun") passwd = driver.find_element_by_xpath("//input[@name='password']").send_keys("zsq5487000!") driver.find_element_by_xpath("//a[@id='dologin']").click() time.sleep(3) time.sleep(2) #切換到首頁 first_page = driver.find_element_by_xpath("//div[.='首頁']") first_page.click() time.sleep(1) #開始寫郵件 driver.switch_to.default_content() time.sleep(1) writeEmail=driver.find_element_by_xpath("//span[text()='寫 信']") writeEmail.click() time.sleep(2) #填寫收件人 driver.switch_to.default_content() driver.find_element_by_class_name("nui-editableAddr-ipt").send_keys("zihailyun@163.com;") driver.find_element_by_class_name("nui-editableAddr-ipt").send_keys("xinxiangyul@163.com;") receive_people = driver.find_element_by_xpath("//*[@title='發給多人時地址請以分號隔開']") receive_people.click() time.sleep(2) #填寫主題 # main_content=driver.find_element_by_xpath("//*[@aria-label='郵件主題輸入框,請輸入郵件主題']") #未起作用 main_content=driver.find_element_by_xpath("//input[@class='nui-ipt-input' and @type='text' and @maxlength='256']").send_keys(u'給大佬發個郵件')# 主題 time.sleep(2) #填寫正文 driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@class="APP-editor-iframe"]')) driver.find_element_by_xpath('//body[@class="nui-scroll"]').send_keys("測試郵件內容,請查收") driver.switch_to.default_content() time.sleep(2) #點擊發送 # driver.find_element_by_xpath("//*[text()='發送']").click() driver.find_element_by_xpath("//span[.='發送']").click() driver.find_elements_by_class_name("nui-btn-text") time.sleep(5) # driver.switch_to.default_content() #設置了一個斷言,看到發送成功這個字段表示發送成功。但好像沒設置好,print不出來。 success = driver.find_element_by_xpath("//*[text()='發送成功']").text if (success == '發送成功'): print('ok') #退出 time.sleep(5) logout_button = driver.find_element_by_xpath("//a[.='退出']") logout_button.click() assert '您已成功退出網易郵箱' in driver.page_source driver.quit()
"""
Xpath元素定位:
1)當前節點下屬性值://
2)若直接只有text ,則用//*[text()=""] //span[.='']
3)定位主題 :<input id="1586878621580_subjectInput" class="nui-ipt-input" type="text" x-webkit-speech="" tabindex="1" maxlength="256">
4)正文的定位://body[@class='nui-scroll' and @contenteditable='true']
"""
# ---------------------