Webdriver之API詳解(3)
前言
前兩篇API連結
ofollow,noindex">https://www.cnblogs.com/linuxchao/p/linuxchao-selenium-apione.html
https://www.cnblogs.com/linuxchao/p/linuxchao-selenium-apitwo.html
①操作多選的選擇列表
被測HTML程式碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>操作多選列表</title> </head> <body> <select name="fruit" size="6" multiple=true> <option id="peach" value="taozi">桃子</option> <option id="watermelon" value="xigua">西瓜</option> <option id="orange" value="juzi">橘子</option> <option id="kiwifruit" value="nihoutao">獼猴桃</option> <option id="maybush" value="shanzha">山楂</option> <option id="litchi" value="lizhi">荔枝</option> </select> </body> </html>
呼叫API例項程式碼
1def testMultipleOptions(self): 2from selenium.webdriver.support.ui import Select 3import time 4self.driver.get(r'file:///C:/Users/v-xug/Desktop/multipleOptions.html') 5select_element = Select(self.driver.find_element_by_xpath('//select')) 6# 通過序號選擇第一個元素 7select_element.select_by_index(0) 8# 通過文字選擇山楂 9select_element.select_by_visible_text('山楂') 10# 通過選項的value屬性值選擇value=獼猴桃 11select_element.select_by_value('nihoutao') 12# 列印所有選中文字 13for option in select_element.all_selected_options: 14print(option.text) 15# 再次選中3個選項 16select_element.select_by_index(1) 17select_element.select_by_value('juzi') 18select_element.select_by_visible_text('荔枝') 19# 取消3個選項 20select_element.deselect_by_index(0) 21select_element.deselect_by_value('nihoutao') 22select_element.deselect_by_visible_text('山楂') 例項程式碼
1 桃子 2 獼猴桃 3 山楂 4 . 5 ---------------------------------------------------------------------- 6 Ran 1 test in 10.988s 7 8 OK 9 10 Process finished with exit code 0 輸出
說明:執行這段程式碼看到的效果是,先選擇3個選項並列印被選擇的選項的文字值,再次選中3個選項並取消之前被選中的3個選項,對於可以多選的操作列表,上面的幾個方法是很實用的,當然實際中可能遇見各種不同的情況,還需多積累經驗對不同問題用不同方法。
②操作可以輸入的下拉列表(輸入的同時模擬按鍵)
被測HTML程式碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>操作可輸入下拉列表,輸入的同時模擬按鍵</title> </head> <body> <div style="position:relative;"> <input list="pasta" id="select"> <datalist id="pasta"> <option>Bavette</option> <option>Rigatoni</option> <option>Fiorentine</option> <option>Gnocchi</option> <option>Tagliatelle</option> <option>Penne lisce</option> <option>Pici</option> <option>Pappardelle</option> <option>Spaghetti</option> <option>Cannelloni</option> <option>Cancl</option> </datalist> </div> </body> </html>
呼叫API例項程式碼
1def testInputSelect(self): 2self.driver.get(r'file:///C:/Users/v-xug/Desktop/inputselect.html') 3from selenium.webdriver.common.keys import Keys 4inputselect = self.driver.find_element_by_id('select') 5inputselect.clear() 6import time 7time.sleep(1) 8# 輸入的同時按下箭頭鍵 9inputselect.send_keys('c', Keys.ARROW_DOWN) 10time.sleep(1) 11inputselect.send_keys(Keys.ARROW_DOWN) 12time.sleep(1) 13inputselect.send_keys(Keys.ENTER) 14time.sleep(3) 操作可輸入下拉列表
說明:執行這段程式碼可以看到輸入框輸入c的同時下拉選項會篩選出資料,且選中篩選出的第一項,但是在某些瀏覽器中不會看到效果(我寫完執行時看到的效果就沒有)。keys模組提供了很多其他的模擬按鍵,可以通過dir()檢視Keys的功能
③操作單選框
被測HTML程式碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>操作單選框</title> </head> <body> <form> <input type="radio" name="fruit" value="berry" />草莓</input> <br /> <input type="radio" name="fruit" value="watermelon" />西瓜</input> <br /> <input type="radio" name="fruit" value="orange" />橙子</input> </form> </body> </html>
呼叫API例項程式碼
1def testRadio(self): 2import time 3self.driver.get(r'file:///C:/Users/v-xug/Desktop/radio.html') 4# 定位到草莓選項 5time.sleep(2) 6berry = self.driver.find_element_by_xpath("//input[@value='berry']") 7berry.click() 8# 斷言是否被選中 9self.assertTrue(berry.is_selected()) 10 11if berry.is_selected(): 12# 如果被選中了重新選擇西瓜選項 13watermelon = self.driver.find_element_by_xpath("//input[@value='watermelon']") 14watermelon.click() 15# 斷言草莓未被選中 16self.assertFalse(berry.is_selected()) 17# 查詢所有的選項 18options = self.driver.find_elements_by_xpath("//input[@name='fruit']") 19# 遍歷所有的選項,如果找到orange且未被選中,那麼就選中這項 20for option in options: 21if option.get_attribute('value')=='orange': 22if not option.is_selected(): 23option.click() 例項程式碼
④操作複選框
被測HTML程式碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>操作複選框</title> </head> <body> <form name="form1"> <input type="checkbox" name="fruit" value="berry" />草莓</input> <input type="checkbox" name="fruit" value="watermelon" />西瓜</input> <input type="checkbox" name="fruit" value="orange" />橙子</input> </form> </body> </html>
呼叫API例項程式碼
1def testCheckBox(self): 2self.driver.get(r'file:///C:/Users/v-xug/Desktop/checkbox.html') 3# 選中一個選項並取消 4berry = self.driver.find_element_by_xpath("//input[@value='berry']") 5berry.click() 6# 斷言是否被選中 7self.assertTrue(berry.is_selected()) 8# 取消選中 9if berry.is_selected(): 10berry.click() 11# 遍歷所有的選項並選中所有的選項 12options = self.driver.find_elements_by_xpath("//input[@name='fruit']") 13for option in options: 14if not option.is_selected(): 15option.click() 例項程式碼
⑤斷言頁面原始碼中的關鍵字
被測地址
http://www.baidu.com
1def testAssertIn(self): 2self.driver.get('http://www.baidu.com') 3self.driver.find_element_by_id('kw').send_keys('linux超') 4self.driver.find_element_by_id('su').click() 5import time 6time.sleep(4) 7self.assertIn('linux超', self.driver.page_source, msg='頁面原始碼中不存在該關鍵字') 例項程式碼
說明:有時候會出現頁面存在要斷言的關鍵字,但是結果仍然斷言失敗, 這有可能是由於頁面沒有載入完全就開始斷言語句, 導致要斷言的內容在頁面原始碼中找不到。
⑥對當前瀏覽器視窗截圖
被測地址
http://www.baidu.com
1def testScreenShot(self): 2self.driver.get('http://www.baidu.com') 3try: 4# 使用get_screenshot_as_file(filename)方法,對瀏覽器當前開啟的頁面截圖,並儲存在當前目錄下 5self.driver.get_screenshot_as_file('baidu.png') 6except IOError as e: 7print(e) 例項程式碼
截圖
說明:呼叫截圖函式get_screenshot_as_file()截圖成功後會返回True,如果發生了IOError異常,會返回False。函式中傳遞的引數可以是絕對路徑也可以是相對路徑;當自動化測試過程中,未實現預期結果,可以將頁面截圖儲存,方便更快速地定位問題。
⑦拖拽頁面元素
被測HTML地址
http://jqueryui.com/resources/demos/draggable/scroll.html
呼叫API例項程式碼
1def testDragDrop(self): 2import time 3self.driver.get(r'http://jqueryui.com/resources/demos/draggable/scroll.html') 4element1 = self.driver.find_element_by_id('draggable') 5element2 = self.driver.find_element_by_id('draggable2') 6element3 = self.driver.find_element_by_id('draggable3') 7from selenium.webdriver import ActionChains 8action = ActionChains(self.driver) 9# 把第一個元素拖拽到第二個元素的位置 10action.drag_and_drop(element1, element2).perform() 11# 把第三個元素拖拽10個畫素,拖拽2次 12for i in range(2): 13action.drag_and_drop_by_offset(element3,10,10).perform() 14time.sleep(2) 15action.release() 例項程式碼
說明:ActionChains模組在前面已經涉及到過了,所有的和滑鼠操作有關的動作都需要使用此模組模擬
⑧模擬鍵盤單個按鍵操作
被測HTML地址
http://www.sogou.com
呼叫API例項程式碼
1def testSingleKey(self): 2import time 3self.driver.get('http://www.sogou.com') 4query = self.driver.find_element_by_id('query') 5# 匯入模擬按鍵模組 6from selenium.webdriver.common.keys import Keys 7# 輸入框傳送一個f12按鍵 8query.send_keys(Keys.F12) 9time.sleep(2) 10# 輸入框中輸入搜尋內容並按下回車鍵 11query.send_keys('selenium') 12query.send_keys(Keys.ENTER) 13time.sleep(2) 例項程式碼
說明:有些電腦執行這個程式碼可能看不到效果,因為有的電腦的F12鍵 是需要和Fn組合才能生效的。
總結:今天的整理到此結束,說實話我不知道對讀到我文章的人幫助有多大,但是對我個人而言是又經歷了一次知識的梳理。把之前忘記的也都慢慢的想起來了,雖然每個例項看著都挺簡單的,其實耗費了我很多精力和時間,因為我想讓讀到我部落格的人只看一次程式碼只執行一次例項就能知道實現的是什麼功能,能把這個功能應用到複雜的測試場景中。其實這也是我自己的一次自我總結把!(最後說一句,我之前是想把每個例項執行的現象用視訊記錄下來,但是當我記錄完之後,並嵌入到文章後視訊根本不顯示,也不知道什麼原因,我是有js許可權的。有人知道什麼原因的話,給我留言或者加我qq教我一下,非常感謝!)