WebDriver高阶API(4)
WebDriver高阶API(4)
8、右键另存为下载文件
AutoIt脚本:
;ControlFocus("title","text",controlID)
;表示将焦点切换到标题为title窗体中的controlID上
;Edit1表示第一个可以编辑的实例
;title表示弹出的Window窗口标题,不同浏览器的标题可能不一样
;等待2秒
Sleep(2000)
;将要下载的文件名及路径写入Edit1编辑框中
ControlSetText("另存为","", "Edit1", "d:\iDownload\Firefox Setup 35.0b8.exe")
Sleep(2000)
;点击窗体中的第一个按钮,也就是保存按钮
ControlClick("另存为","","Button1")
将AutoIt脚本转换为.exe文件
下载路径需要是存在的路径
#encoding=utf-8 import unittest import time import os from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver import ActionChains import win32con import win32api VK_CODE = { "enter":0x0D, "down_arrow":0x28 } #键盘键按下 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 TestDemo(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(executable_path = "D:\\chromedriver") def test_dataPickerByRightKey(self): url = "http://ftp.mozilla.org/pub/firefox/releases/35.0b8/win32/zh-CN/" #访问被测网页 self.driver.get(url) #窗口最大化 self.driver.maximize_window() #暂停5秒,以防页面有一些多余的弹窗占据焦点 time.sleep(5) #找到文本内容为“Firefox Setup 35.0b8.exe”的超链接元素 a = self.driver.find_element_by_link_text("Firefox Setup 35.0b8.exe") time.sleep(2) #在找到的链接元素上模拟单击鼠标右键,以便调出“链接另存为”选项的菜单 ActionChains(self.driver).context_click(a).perform() #暂停2秒,以防命令执行太快 time.sleep(2) for i in range(4): #循环按4次下箭头,将焦点切换到“链接另存为”选项上 #不同浏览器此选项位置可能不同 # a.send_keys(Keys.DOWN) keyDown('down_arrow') keyUp("down_arrow") print i time.sleep(2) time.sleep(2) #当焦点切换到“链接另存为”选项上后,模拟按回车键 #调出保存下载文件路径的Window窗体 keyDown("enter") print "enter-down" keyUp("enter") print "enter-up" time.sleep(3) #通过执行AutoIt编写的操作弹窗的Windows文件保存窗体 #完成文件保存路径的设置 os.system("D:\\test\\download_file.exe") #等待文件下载完成,根据各自的网络宽带情况设定等待相应的时间 time.sleep(50) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
9、操作日期控件
1)直接输入日期
#encoding=utf-8 import unittest,time,traceback from selenium import webdriver 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 class TestDemo(unittest.TestCase): def setUp(self): #启动浏览器 #谷歌浏览器执行失败 # self.driver = webdriver.Chrome(executable_path="D:\\chromedriver") # self.driver.maximize_window() self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer") def test_datePicker(self): url = "http://jqueryui.com/resources/demos/datepicker/other-months.html" #访问网站 self.driver.get(url) time.sleep(5) try: #创建一个显式等待对象 wait = WebDriverWait(self.driver,10,0,0.2) #显式等待判断被测页面上的日期输入杠是否可见并且能被单击 wait.until(EC.element_to_be_clickable((By.ID,"datepicker"))) except TimeoutException,e: #捕获TimeoutException异常 print traceback.print_exc() except NoSuchElementException,e: print traceback.print_exc() except Exception,e: print traceback.print_exc() else: #找到被测页面上的日期输入框页面元素 dateInputBox = self.driver.find_element_by_id("datepicker") #查找到日期输入框,直接输入指定格式的日期字符串 #就可以变相模拟地日期控件上进行选择了 dateInputBox.send_keys("12/24/2018") time.sleep(3) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
2)点击选择
#encoding=utf-8 import unittest,time,traceback from selenium import webdriver 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 class TestDemo(unittest.TestCase): def setUp(self): #启动浏览器 self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer") def test_datePicker(self): url = "http://jqueryui.com/resources/demos/datepicker/other-months.html" #访问网址 self.driver.get(url) time.sleep(5) try: #创建一个显式等待对象 wait = WebDriverWait(self.driver,10,0,0.2) #显式等待判断被测页面上的日期输入 框是否可见并且可以单击 wait.until(EC.element_to_be_clickable((By.ID,"datepicker"))) except TimeoutException,e: print traceback.print_exc() except NoSuchElementException,e: print traceback.print_exc() except Exception,e: print traceback.print_exc() else: #查找被测页面上的日期输入框页面元素 dateInputBox = self.driver.find_element_by_id("datepicker") #点击输入框,弹出日期选择模块 dateInputBox.click() time.sleep(1) #找到当月一个日期并点击 self.driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr[3]/td[4]/a').click() time.sleep(3) #上一个月的某天 dateInputBox.click() self.driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/div/a[1]/span').click() self.driver.find_element_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr[2]/td[4]/a').click() time.sleep(3) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()