1七、測試HTML5語言實現的視頻播放器html
#encoding=utf-8 import unittest import time from selenium import webdriver class TestDemo(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(executable_path = "D:\\chromedriver") def test_HTML5VideoPlayer(self): url = "http://www.w3school.com.cn/tiy/loadtext.asp?f=html5_video_simple" #訪問HTML5語言實現的播放器網頁 self.driver.get(url) #打印訪問網頁的頁面源代碼,供讀取學習 print self.driver.page_source #獲取頁面中的video標籤元素對象 videoPlayer = self.driver.find_element_by_tag_name("video") #使用Javascript語句,經過播放器內部的currentSrc屬性獲取視頻文件的網絡存儲地址 videoSrc = self.driver.execute_script("return arguments[0].currentSrc;",videoPlayer) #打印網頁中視頻存放地址 print videoSrc #斷言視頻存放地址是否符合預期 self.assertEqual(videoSrc,"http://www.w3school.com.cn/i/movie.ogg") #使用Javascript語句,經過播放器內部的duration屬性獲取視頻文件的播放時長 videoDuration = self.driver.execute_script("return arguments[0].duration",videoPlayer) #打印視頻長度 print videoDuration #對獲取到的視頻時長取整,而後斷言是否等於3秒 self.assertEqual(int(videoDuration),3) #使用Javascript語句,經過調用播放器內部的play()方法來播放影片 self.driver.execute_script("return arguments[0].play();",videoPlayer) time.sleep(2) #播放2秒後,使用JavaScript語句,經過調用播放器內部的pause()函數來暫停播放影片 self.driver.execute_script("return arguments[0].pause();",videoPlayer) #暫停3秒,以便人工確認視頻是否已被暫停 time.sleep(3) #將暫停視頻播放頁面進行截屏,並保存到D盤的videoPlay_pause.jpg文件 self.driver.save_screenshot("D:\\videoPlay_pause.jpg") def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
1八、在HTML5的畫布元素上進行繪畫操做html5
#encoding=utf-8 import unittest import time from selenium import webdriver class TestDemo(unittest.TestCase): def setUp(self): self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer") def test_HTML5Canvas(self): url = "http://www.w3school.com.cn/tiy/loadtext.asp?f=html5_canvas_line" #訪問指定網址 self.driver.get(url) #調用Javascript語句,在頁面畫布上畫一個紅色的圖案 #getElementById("myCanvas");語句獲取頁面上的畫布元素 #var cxt = c.getContext("2d");設定畫布爲2d #cxt.fillStyle = "#FF0000"; 設定填充顏色爲#FF0000 #cxt.fillRect(0,0,150,150);在畫布上繪製矩形 self.driver.execute_script("var c = document.getElementById('myCanvas');" + "var cxt = c.getContext('2d');" + "cxt.fillStyle = '#FF0000';" +"cxt.fillRect(0,0,150,150);") time.sleep(3) #將繪製的紅色矩形頁面進行截屏,並保存爲D盤的HTML5Canvas.jpg self.driver.save_screenshot("D:\\HTML5Canvas.png") def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
1九、使用Chrome瀏覽器自動將文件下載到指定路徑web
#encoding=utf-8 import unittest,time from selenium import webdriver class TestDemo(unittest.TestCase): def setUp(self): #建立Chrome瀏覽器配置對象實例 chromeOptions = webdriver.ChromeOptions() #設定下載文件的保存目錄爲D盤的iDownload目錄 #若是該目錄不存在,將會自動建立 prefs = {"download.default_directory":"D:\\iDownload"} #將自定義設置添加到Chrome配置實例對象中 chromeOptions.add_experimental_option("prefs",prefs) #啓動帶有自定義配置的Chrome瀏覽器 self.driver = webdriver.Chrome(executable_path = "D:\\chromedriver",chrome_options = chromeOptions) def test_downloadFileByChrome(self): url = "https://pypi.org/project/selenium/#files" #訪問將要下載文件的網址 self.driver.get(url) #找到要下載的文件連接頁面元素,並單擊進行下載 self.driver.find_element_by_partial_link_text("selenium-3.141.0.tar.gz").click() #等待50秒,以便文件下載完成 time.sleep(50) def tearDown(self): self.driver.quit() if __name__ == "__main": unittest.main()
20、禁用Chrome瀏覽器的PDF、Flash、Image插件chrome
#encoding=utf-8 import unittest,time from selenium import webdriver from selenium.webdriver.chrome.options import Options class TestDemo(unittest.TestCase): def setUp(self): #建立Chrome瀏覽器的一個Options實例對象 chrome_options = Options() #設置Chrome瀏覽器禁用PDF、Flash和Image插件 profile = {"plugins.plugins_disabled":["Chrome PDF Viewer"], "plugins.plugins_disabled":["Adobe Flash Player"], "profile.managed_default_content_settings.images":2} chrome_options.add_experimental_option("prefs",profile) #向Options實例中添加禁用擴展插件的設置參數項 chrome_options.add_argument("--disable--extensions") #添加屏蔽--ignore-certificate-errors提示信息的設置參數項 chrome_options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"]) #添加瀏覽器最大化的設置參數項,一啓動就最大化 chrome_options.add_argument("--start-maximized") #啓動帶有自定義設置的Chrome瀏覽器 self.driver = webdriver.Chrome(executable_path = "D:\\chromedriver",chrome_options = chrome_options) def test_forbidPDFFlashImageInChrome(self): #訪問愛奇藝 self.driver.get("http://www.iqiyi.com") #等待50秒,期間能夠看到因爲禁用了Flash插件,致使須要Flash支持的內容沒法正常展現 time.sleep(10) #查看PDF和Flash插件禁用狀況 self.driver.get("chrome://plugins/") time.sleep(10) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
2一、禁用IE的保護模式canvas
#encoding=utf-8 import unittest,time from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities class TestDemo(unittest.TestCase): def setUp(self): caps = DesiredCapabilities.INTERNETEXPLORER #將忽略IE保護模式的參數設置爲True caps["ignoreProtectedModeSettings"] = True #啓動帶有自定義設置的IE瀏覽器 self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer",capabilities = caps) def test_closeIEProtectedMode(self): #訪問百度 self.driver.get("http://www.baidu.com") time.sleep(2) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()