web自动化:web控件交互操作/多窗口处理/网页frame
序:web自動化,除了需要定位網(wǎng)頁元素外,在定位到元素后,一般都需要執(zhí)行一系列操作,最常用的就是單擊click,另外還包括輸入文件、右鍵點(diǎn)擊,頁面滑動,表單操作等
selenium webdriver api指導(dǎo)文檔:
https://selenium-python.readthedocs.io/api.html
selenium提供的操作接口:
ActionChains:執(zhí)行PC端的鼠標(biāo)點(diǎn)擊,雙擊,右鍵,拖拽等操作
TouchActions:模擬PC和移動端的點(diǎn)擊,滑動,拖拽,多點(diǎn)觸控等多種手勢操作
ActionChains用法:
包含的一些基本操作:
click(on_element=None):模擬左鍵點(diǎn)擊,如果不傳入element,那么點(diǎn)擊鼠標(biāo)當(dāng)前位置
click_and_hold(on_element=None):點(diǎn)擊并不釋放
context_click(on_element=None):模擬右鍵點(diǎn)擊
double_click(on_element=None):模擬雙擊
drag_and_drop(source, target):拖動然后移動到另外一個(gè)位置放下,傳入兩個(gè)元素,第一個(gè)元素位置和第二個(gè)元素位置
key_down(value, element=None)
key_up(value, element=None)
ActionChains執(zhí)行原理
調(diào)用ActionChains的方法時(shí),不會立即執(zhí)行,而是將所有的操作,按順序存放在一個(gè)隊(duì)列里,當(dāng)調(diào)用perform()方法時(shí),才會以此執(zhí)行 基本用法: 1、生成一個(gè)動作action = ActionChains(driver) 2、添加動作方法1 action.方法1 3、添加動作方法2 action.方法2 4、調(diào)用action.perform()方法執(zhí)行 具體寫法: 1、鏈?zhǔn)綄懛?ActionChains(driver).move_to_element(element).click(element).perform() 2、分布寫法 action = ActionChains(driver) action.move_to_element(element) action.click(element) action.perform()1、模擬點(diǎn)擊、右鍵、雙擊等操作
element_click = self.driver.find_element_by_xpath('xpath')element_doubleclick = self.driver.find_element_by_xpath('xpath')element_rightclick = self.driver.find_element_by_xpath('xpath')action = ActionChains(self.driver)action.click(element_click)action.double_click(element_doubleclick)action.context_click(element_rightclick)sleep(3)action.perform()2、模擬鼠標(biāo)移動到某個(gè)元素上
def test_movetoelement(self):self.driver.get("https://www.baidu.com/")self.driver.maximize_window()element = self.driver.find_element_by_xpath('//*[@id="u1"]/span')action = ActionChains(self.driver)action.move_to_element(element)action.perform()sleep(5)3、模擬按鍵
模擬按鍵方法有多種:1、用win32api來實(shí)現(xiàn) 2、能用SendKeys來實(shí)現(xiàn) 3、能用selenium的webelement對象的send_keys()方法實(shí)現(xiàn) 4、ActionChains類也提供了幾個(gè)模擬按鍵的方法 ActionChains用法介紹 1、action = ActionChains(self.driver) 2、action.send_keys(Keys.BACK_SPACE) 3、或者action.key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL) 4、action.perform() self.driver.get()element = self.driver.find_element_by_xpath("")element.click()action = ActionChains(self.driver)action.send_keys("username").pause(1)action.send_keys(Keys.SPACE).pause(1)action.send_keys("tom").pause(1)action.send_keys(Keys.BACK_SPACE).perform()TouchActions用法:
官網(wǎng)文檔:
https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.touch_actions.html?highlight=touchactions
TouchAction類似與ActionChains,ActionChains只是針對PC端程序鼠標(biāo)模擬的一系列操作,對h5頁面操作是無效的 TouchAction可以對h5頁面操作,可以實(shí)現(xiàn)點(diǎn)擊、滑動、多點(diǎn)觸控、以及模擬手勢的各種操作 手勢控制: tap---在指定元素上單擊 double_tap---在指定元素上雙擊 tap_and_hold---在指定元素上點(diǎn)擊但不釋放 move---手勢移動指定偏移(不釋放) release---釋放手勢 scroll---手勢點(diǎn)擊并滾動 scroll_form_element---從某個(gè)元素位置開始手勢點(diǎn)擊并滾動(向下滑動為負(fù)數(shù),向上滑動為正數(shù)) long_press---長按元素 flick---手勢滑動 flick_element---從某個(gè)元素位置開始手勢滑動(向上滑動為負(fù)數(shù),向下滑動為正數(shù)) perform---執(zhí)行 class TestTouchActions:def setup(self):option = webdriver.ChromeOptions()option.add_experimental_option('w3c', False)self.driver = webdriver.Chrome(options=option)self.driver.implicitly_wait(5)self.driver.maximize_window()def teardown(self):self.driver.quit()def test_touchactions_scrollbutton(self):'''打開百度網(wǎng)頁在搜索框中輸入‘selenium’滑動到底部,點(diǎn)擊下一頁按鈕關(guān)閉chorme瀏覽器'''self.driver.get("https://www.baidu.com")ele = self.driver.find_element_by_id('kw').send_keys('selenium')ele_su = self.driver.find_element_by_id('su')action = TouchActions(self.driver)action.tap(ele_su)action.perform()# 滑動到底部action.scroll_from_element(ele_su, 0, 10000).perform()sleep(2)表單操作
表單時(shí)一個(gè)包含表單元素的區(qū)域,是允許用戶在表單中(如文本域、下拉列表、單選框、復(fù)選框等)輸入信息的元素
表單使用表單標(biāo)簽(<form>)定義,例如 :<form><input/></form>
操作表單元素的步驟:
1、定位到表單元素
2、操作元素:清空,輸入或者點(diǎn)擊等
多窗口處理
當(dāng)我們在網(wǎng)頁上點(diǎn)擊一個(gè)按鈕,如果新開一個(gè)網(wǎng)頁窗口去實(shí)現(xiàn)頁面跳轉(zhuǎn),然后需要在新頁面上操作,這種情況該如何處理
想在新頁面上操作,就得先切換窗口,獲取窗口的唯一標(biāo)識用句柄表示,所以只需要切換句柄,就可以在多個(gè)頁面靈活操作
多窗口處理方法:
1、先獲取到當(dāng)前的窗口句柄:driver.current_window_handle
2、再獲取到所有窗口的句柄:driver.window_handles
3、判斷是否是想要操作的窗口,如果是,就可以對窗口進(jìn)行操作,如果不是,跳轉(zhuǎn)到另外一個(gè)窗口:driver.switch_to.window,對另一個(gè)窗口進(jìn)行操作
多窗口切換案例:打開百度頁面,點(diǎn)擊登錄,在彈框中點(diǎn)擊立即注冊(打開一個(gè)新頁面,在用戶名處輸入username),返回剛才的登錄頁面,點(diǎn)擊用戶名登錄
def test_windows(self):self.driver.get("https://www.baidu.com/")self.driver.find_element_by_xpath('//*[@id="u1"]/a').click()print(self.driver.current_window_handle)self.driver.find_element_by_link_text('立即注冊').click()print(self.driver.window_handles)print(self.driver.current_window_handle)windows = self.driver.window_handlesself.driver.switch_to.window(windows[1])self.driver.find_element_by_id("TANGRAM__PSP_4__userName").send_keys("username")sleep(3)self.driver.switch_to.window(windows[0])self.driver.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').click()sleep(3)網(wǎng)頁frame處理
當(dāng)一個(gè)網(wǎng)頁里面有多個(gè)html頁面組成時(shí),比如網(wǎng)頁內(nèi)嵌,這時(shí)就會有多個(gè)frame,這種場景又該如何處理
在web自動化中,如果一個(gè)元素定位不到,那么很大可能是在iframe中
什么是frame:frame是html中的框架,在html中,所謂的框架就是可以在同一個(gè)瀏覽器中顯示不止一個(gè)頁面,基于html的框架,又分為垂直框架和水平框架(cols,rows)
frame分類:
frame標(biāo)簽包含frameset、frame、iframe三種,frameset和普通的標(biāo)簽一樣,不會影響元素定位
而frame與iframe對selenium定位而言是一樣的,selenium有一組方法可以對frame進(jìn)行操作
frame的存在方式有兩種:一種是嵌套的,一種是不嵌套的
切換frame:
driver.switch_to.frame()? 根據(jù)元素id或者index切換frame
driver.switch_to.default_content()? 切換到默認(rèn)frame
driver.switch_to.parent_frame()? 切換到父級frame
處理未嵌套的iframe:
driver.switch_to.frame(frame_id)
driver.switch_to.frame(frame_index) frame無id的時(shí)候根據(jù)索引來處理,索引從0開始,driver.switch_to_frame(0)
處理嵌套的iframe:
對于嵌套的先進(jìn)入iframe的父節(jié)點(diǎn),再進(jìn)到子節(jié)點(diǎn),然后可以對子節(jié)點(diǎn)里面的對象進(jìn)行處理和操作
driver.switch_to.frame("父節(jié)點(diǎn)")
driver.switch_to.frame("子節(jié)點(diǎn)")
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的web自动化:web控件交互操作/多窗口处理/网页frame的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android lomo设计与实现,拍静
- 下一篇: 01—C语言基本语句