1. 程式人生 > >python webdriver 登陸163郵箱給QQ郵箱發送一個郵件,顯示等待

python webdriver 登陸163郵箱給QQ郵箱發送一個郵件,顯示等待

try body component cte elf them rac send pri

#encoding=utf-8

import unittest

import time

from selenium import webdriver

from selenium.webdriver import ActionChains

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.common.exceptions import TimeoutException, NoSuchElementException

import traceback

class Visit163ByFirefox(unittest.TestCase):

def setUp(self):

#啟動IE瀏覽器

self.driver=webdriver.Firefox(executable_path=‘d:\\geckodriver‘)

#self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")

def test_HandleIFrame(self):

url = "https://mail.163.com/"

# 訪問自動以測試網頁

self.driver.get(url)

try:

#顯示等待

wait=WebDriverWait(self.driver,15,0.2)

#切換frame

self.driver.switch_to.frame(self.driver.find_element_by_xpath("//*[@id=‘x-URS-iframe‘]"))

#self.driver.switch_to.frame("x-URS-iframe")

#顯示等待獲取用戶名輸入框元素

name=wait.until(lambda x:x.find_element_by_xpath("//*[@class=‘j-inputtext dlemail‘]"))

name.send_keys("xxxxx")

#顯示等待獲取密碼輸入框元素

passwd=wait.until(lambda x:x.find_element_by_xpath("//*[@class=‘j-inputtext dlpwd‘]"))

passwd.send_keys("xxxxx")

#登錄

login=wait.until(lambda x:x.find_element_by_id("dologin"))

login.click()

#切回到默認framae

self.driver.switch_to.default_content()

#獲取寫信按鈕元素

iwrite=wait.until(lambda x:x.find_element_by_xpath("//li[@id=‘_mail_component_74_74‘]/*[@class=‘oz0‘]"))

iwrite.click()

#獲取收件人輸入框元素

receiver=wait.until(lambda x:x.find_element_by_xpath("//input[@class=‘nui-editableAddr-ipt‘ and @role=‘combobox‘]"))

#receiver.click()

receiver.send_keys("[email protected]")

#獲取主題輸入框元素

theme=wait.until(lambda x:x.find_element_by_xpath("//input[@class=‘nui-ipt-input‘ and @type=‘text‘ and @maxlength=‘256‘]"))

#theme.click()

theme.send_keys(u"這是夏曉旭的第一個自動化發郵件腳本!")

#切到寫信內容部分的frame

self.driver.switch_to.frame(self.driver.find_element_by_xpath("//*[@class=‘APP-editor-iframe‘]"))

#獲取寫信區域的元素

editBox = wait.until(lambda x:x.find_element_by_xpath("/html/body"))

editBox.click()

editBox.send_keys(u‘這是夏曉旭的第一個自動化發郵件腳本!‘)

#切回到默認frame

self.driver.switch_to.default_content()

#獲取發送按鈕元素

send=wait.until(lambda x:x.find_element_by_xpath("//*[@class=‘jp0‘]//*[@role=‘button‘]//*[.=‘發送‘]"))

send.click()

except TimeoutException, e:

# 捕獲TimeoutException異常

print traceback.print_exc()

except NoSuchElementException, e:

# 捕獲NoSuchElementException異常

print traceback.print_exc()

except Exception, e:

# 捕獲其他異常

print traceback.print_exc()

if __name__ == ‘__main__‘:

unittest.main()

python webdriver 登陸163郵箱給QQ郵箱發送一個郵件,顯示等待