17792550360
掃描二維碼
關(guān)注卓目鳥學(xué)苑公眾號
掃描二維碼
關(guān)注卓目鳥學(xué)苑公眾號
Selenium是一個用于Web應(yīng)用程序測試的工具,Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣。Python則是強大的腳本語言,已廣泛地應(yīng)用于自動化測試中。本文簡單介紹一下Selenium和Python的使用。1、Pyht ...
Selenium是一個用于Web應(yīng)用程序測試的工具,Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣。Python則是強大的腳本語言,已廣泛地應(yīng)用于自動化測試中。 本文簡單介紹一下Selenium和Python的使用。 1、Pyhton和Selenium的安裝與環(huán)境搭建 (1)Python的安裝 在官網(wǎng)下載:進入Python官方網(wǎng)站:https://www.python.org/downloads/ 下載并安裝最新版本的Python(建議安裝Python3) 。 python3.0以后的版本不需要下載setuptools和pip安裝,已經(jīng)在python中集成了pip,可以在cmd窗口中使用命令: pip -V 進行pip版本查詢。通過pip就可以直接在命令行中安裝自己想要的庫。注意:下載的python一定要記住安裝位置,最好是磁盤根目錄創(chuàng)建一個名為python的目錄專門存放python。 添加環(huán)境變量,也就是python所在的位置,如下圖所示。首先:右鍵此電腦->點擊屬性->高級系統(tǒng)設(shè)置->環(huán)境變量->系統(tǒng)變量選擇Path->新建->填寫python的地址->確定,即可完成環(huán)境變量的添加。 在cmd中輸入python判斷python是否安裝成功 (2)Selenium的安裝 安裝Selenium3.141.0可以使用命令直接下載并安裝,但是也可以通過官網(wǎng)直接下載 并使用pythonsetup.pyinstall進行安裝,這里一定要注意進入到安裝selenium的安裝目錄才能使用此命令,隨后在python中導(dǎo)入selenium,若沒有報錯,則證明安裝selenium成功。 (3)瀏覽器驅(qū)動的下載 由于主流的瀏覽器有三個這里列舉三種瀏覽器的下載地址和啟用的實例,分別為谷歌瀏覽器,火狐瀏覽器和IE瀏覽器的驅(qū)動的下載和運行實例。 1、chromedriver 下載地址:http://chromedriver.storage.googleapis.com/index.html 啟動谷歌瀏覽器 from selenium import webdriver browser = webdriver.Chrome() browser.get('http://www.baidu.com/') 2、Firefox的驅(qū)動geckodriver 下載地址:https://github.com/mozilla/geckodriver/releases/ (切記下對版本) 啟動火狐瀏覽器 from selenium import webdriver browser = webdriver.Firefox() browser.get('http://www.baidu.com/') 3、IE的驅(qū)動IEdriver 下載地址:http://www.nuget.org/packages/Selenium.WebDriver.IEDriver/ 啟動IE瀏覽器 from selenium import webdriver browser = webdriver.Ie() browser.get('http://www.baidu.com/') 2、測試腳本的編寫 安裝好所需的全部環(huán)境就可以進行腳本的編寫了。首先selenium是通過獲取網(wǎng)頁的元素然后對網(wǎng)頁進行控制,首先我們要幫助它找到網(wǎng)頁的元素: Selenium 給我們提供了幾種的定位元素方法: (1) find_element_by_id() 我們可以直接通過英文意思理解這個方法,通過 ID 查找元素,也就是使用頁面里的 id 屬性:id = “”。 (2)find_element_by_name() 通過查找名字的方式,對元素進行定位。 (3)find_elements_by_class_name() 通過查找class_name的方式,對元素進行定位。 (4)find_element_by_tag_name() 通過查找tag_name的方式,對元素進行定位。 (5)find_element_by_link_text() 通過查找頁面的文本信息進行定位。 (6)find_element_by_partial_link_text() 通過模糊文本信息查找元素,有些時候,我們希望定位到一個文本比較長的元素時,我們就可以通過這個方法去定位。 (7)find_element_by_xpath() xpath 元素定位:通過查找元素的路徑去查找元素,由于獲取的方式簡單,所以這種方法是最常用的方法。 3、測試實例列舉 (1)循環(huán)點擊200次呼叫撥號、創(chuàng)建會議、會議室、通訊錄、最近呼叫和系統(tǒng)狀態(tài)。 from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver import ActionChainsfrom selenium.webdriver.common.action_chains import ActionChainsfrom time import sleepbrowser = webdriver.Chrome()browser.get('http://172.16.235.13/')print(browser.current_url)sleep(2)browser.find_element_by_id("loginAccount").send_keys("admin")sleep(2)browser.find_element_by_id("lpassword").send_keys("admin123")browser.find_element_by_id("loginSubmit").click()sleep(2)i = 0while i<200:browser.find_element_by_link_text("撥號呼叫").click() sleep(2) browser.find_element_by_link_text("創(chuàng)建會議").click() sleep(2) browser.find_element_by_link_text("會議室").click() sleep(2) browser.find_element_by_link_text("通訊錄").click() sleep(2) browser.find_element_by_link_text("最近呼叫").click() sleep(2) browser.find_element_by_link_text("系統(tǒng)狀態(tài)").click() sleep(2) i+=1 (2)點對點呼叫掛斷,循環(huán)十次,呼叫之后截圖保存在路徑C:/Users/zhd/Desktop/JMeter/test/%s.png并命名為當(dāng)前系統(tǒng)時間。 from selenium import webdriverfrom time import sleepbrowser = webdriver.Chrome()browser.get('http://172.16.235.13/')#輸入本終端的IP地址print(browser.current_url)#輸出到控制臺當(dāng)前測試終端的IP地址sleep(2)#增加延時2sbrowser.find_element_by_id("loginAccount").send_keys("admin")#輸入用戶名sleep(2)browser.find_element_by_id("lpassword").send_keys("admin123")#輸入密碼browser.find_element_by_id("loginSubmit").click()#點擊登錄sleep(5) i = 0while i<200:#循環(huán)200次browser.switch_to.frame("contentFrame")#選擇主框架 browser.find_element_by_id("callinfoInputId").send_keys("172.16.235.24")#輸入呼叫的IP地址 sleep(2) browser.find_element_by_id("callid").click()#點擊呼叫 sleep(30) browser.get_screenshot_as_file("C:/Users/zhd/Desktop/JMeter/test/%s.png" % datetime.datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3])#截圖并保存到指定目錄browser.switch_to.frame("contentFrame") browser.find_element_by_id("endconf").click()#點擊結(jié)束會議 sleep(2) browser.get_screenshot_as_file("C:/Users/zhd/Desktop/JMeter/test/%s.png" % datetime.datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3])#截圖并保存到制定目錄 browser.switch_to.parent_frame()#返回父級目錄browser.find_element_by_xpath('//*[@id="alertid"]/div[1]/table/tbody/tr[2]/td[2]/div[2]/div[2]/div/div[1]/div[2]').click()#點擊確定 browser.get_screenshot_as_file("C:/Users/zhd/Desktop/JMeter/test/%s.png" % datetime.datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3])#截圖并保存在制定的目錄 (3)循環(huán)創(chuàng)建多點會議并退出會議 from selenium import webdriver from time import sleep import datetime browser = webdriver.Chrome(r'C:\Users\tiancizhao\Desktop\JMeter\chromedriver_win32\chromedriver.exe') browser.get('http://192.168.55.56/')#輸入本終端的IP地址 sleep(2)#增加延時2s browser.find_element_by_id("loginAccount").send_keys("admin")#輸入用戶名 sleep(2) browser.find_element_by_id("lpassword").send_keys("111111qq")#輸入密碼 browser.find_element_by_id("loginSubmit").click()#點擊登錄 try: browser.find_element_by_xpath( '//*[@id="alertid"]/div[1]/table/tbody/tr[2]/td[2]/div[2]/div[2]/div/div[1]/div[2]').click() except: pass sleep(5) while True: browser.find_element_by_link_text("創(chuàng)建會議").click() #切換到創(chuàng)會的界面 browser.switch_to.frame("contentFrame")#切換到內(nèi)容框架 browser.find_element_by_id("CreatConfBtn").click()#點擊創(chuàng)建多點會議 sleep(300) #等待300s browser.get_screenshot_as_file("C:/Users/zhd/Desktop/test/%s/Desktop/test/%s.png" % datetime.datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3])#截圖并保存在制定的目錄 sleep(3) browser.switch_to.frame("contentFrame")#切換到內(nèi)容框架 browser.find_element_by_id("leaveconf").click()#點擊退出會議 sleep(3) browser.switch_to.parent_frame() browser.get_screenshot_as_file("C:/Users/zhd/Desktop/test/%s/Desktop/test/%s.png" % datetime.datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3])#截圖并保存在制定的目錄 sleep(3) browser.find_element_by_xpath('/html/body/div[3]/div[1]/table/tbody/tr[2]/td[2]/div[2]/div[2]/div/div[1]/div[2]').click()#點擊確定結(jié)束會議 sleep(5) 4、總結(jié) 在操作的過程中注意以下幾點: 1、查找網(wǎng)頁元素的時候,注意網(wǎng)頁中使用frame的情況,首先要使用browser.switch_to.frame("contentFrame")命令進入frame中,才能選中所需的元素。使用之后要記得返回上一級frame,browser.switch_to.parent_frame()。Webmtc主要使用id選擇和xpath獲取元素,webmtc網(wǎng)頁中class使用的比較少。 2、還要注意頁面刷新之后代碼也會找不到frame,比如呼叫建立之后頁面會刷新,多點會議創(chuàng)建之后頁面會刷新,點擊設(shè)置界面和呼叫界面頁面也會刷新。 |
分享本篇文章給更多人:
2020-05-27
2020-02-24
2020-05-27
2022-12-05
2020-05-27
請發(fā)表評論