制作简易网站,深色网站,window2003iis建好的网站,动漫设计与制作主修课程点此获取更多相关资料 本文为霍格沃兹测试开发学社学员学习笔记分享 原文链接#xff1a;https://ceshiren.com/t/topic/27048 一、多窗口处理.
1.1、多窗口简介 点击某些链接#xff0c;会重新打开⼀个窗⼜#xff0c;对于这种情况#xff0c;想在新页⾯上操作#xff0…点此获取更多相关资料 本文为霍格沃兹测试开发学社学员学习笔记分享 原文链接https://ceshiren.com/t/topic/27048 一、多窗口处理.
1.1、多窗口简介 点击某些链接会重新打开⼀个窗⼜对于这种情况想在新页⾯上操作就 得先切换窗⼜了。 获取窗⼜的唯⼀标识⽤句柄表⽰所以只需要切换句柄就可以在多个页⾯灵 活操作了 1.2、多窗口处理流程
先获取当前窗口的句柄driver.current_window_handle再获取所有的窗口句柄driver.windows_handles然后判断当前窗口是否为需要操作的窗口如果不是则切换到下一个窗口如果是则在当前窗口进行操作 def test_switch_window(self):窗口切换操作# 1、打开百度self.driver.get(https://www.baidu.com)print(self.driver.title, self.driver.current_window_handle)# 2、打开搜狗self.driver.switch_to.new_window()self.driver.get(https://www.sougou.com)print(self.driver.title, self.driver.current_window_handle)# 3、打开hao360self.driver.switch_to.new_window()self.driver.get(https://hao.360.com/)print(self.driver.title, self.driver.current_window_handle)# 4、打开测试人self.driver.switch_to.new_window()self.driver.get(https://ceshiren.com)print(self.driver.title, self.driver.current_window_handle)handles self.driver.window_handlesprint(handles)self.driver.switch_to.window(handles[0])self.driver.switch_to.window(handles[-1])print(self.driver.title)二、多网页frame处理
2.1、frame简介 在web自动化中如果一个元素始终无法定位那么很有可能是frame中 什么是frame呢 frame是html的框架所谓框架就是可以在同一个页面显示不止一个区域基于html框架又可以分为垂直框架和水平框架cols,rows frame分类 frame标签分为framesetifame、frame三种frameset和普通的标签一样不会影响正常的元素定位可以使用index、id、name、webelement等方式定位到frameframe、iframe相当于selenium而言则需要进行一些特殊的操作后才能到定位到元素
2.2、多frame切换 frame存在两种 一种嵌套的一种未嵌套的 切换frame driver.swich_to.frame()根据元素id、index切换framedriver.switch_to.default_content()切换到默认的framedeiver.switch_to.parent_frame()切换到父级frame
未嵌套的frame
driver.switch_to.frame(‘frame的id’)有id时优先使用iddriver.switch_to.frame(‘frame-index’)没有id的时间根据索引来处理索引从0开始
嵌套的frame
对于嵌套的frame则先进入到frame的父节点再进到子节点然后可以就可以子节点中的元素对象进行操作了driver.switch_to.frame(“父节点”)driver.switch_to.frame(“子节点”) def test_switch_frame(self):# ❖ 打开包含frame的web页⾯ https://www.runoob.com/try/try.php?filenamejqueryui-api-droppable# ❖ 打印’请拖拽我’元素的⽂本# ❖ 打印’点击运⾏’元素的⽂self.driver.get(https://www.runoob.com/try/try.php?filenamejqueryui-api-droppable)self.driver.switch_to.frame(iframeResult)ele01 self.driver.find_element(By.ID, draggable)print(ele01.text)ele02 self.driver.find_element(By.ID, droppable)print(ele02.text)self.action.drag_and_drop(ele01, ele02).perform()time.sleep(3)self.driver.switch_to.alert.accept()# self.driver.switch_to.default()# self.driver.switch_to.parent_frame()