#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/"
# Access automatic to test Web pages
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 "This is Xia Xiaoxu's first automated email script!) ")
#切到写信内容部分的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 ' This is Xia Xiaoxu's first automated email script!) ‘)
#切回到默认frame
Self.driver.switch_to.default_content ()
#获取发送按钮元素
Send=wait.until (Lambda x:x.find_element_by_xpath ("//*[@class = ' jp0 ']//*[@role = ' button ']//*[.= ' send ']"))
Send.click ()
Except TimeoutException, E:
# Capturing TimeoutException Exceptions
Print Traceback.print_exc ()
Except Nosuchelementexception, E:
# Capturing Nosuchelementexception Exceptions
Print Traceback.print_exc ()
Except Exception, E:
# Catch Other exceptions
Print Traceback.print_exc ()
if __name__ = = ' __main__ ':
Unittest.main ()
Python webdriver Login 163 mail to QQ mailbox send a message, show wait