网站代理违法吗,hyper cache wordpress,商城微发布,wordpress在线储存1.切换窗口
适用场景#xff1a;点击按钮后#xff0c;重新打开一个窗口#xff0c;想要在新的窗口定位操作#xff0c;就需要切换窗口
原理#xff1a;获取窗口的唯一标识就是句柄#xff0c;获取到句柄#xff0c;就可以切换到对应的窗口了
处理方法#xff1a;
…1.切换窗口
适用场景点击按钮后重新打开一个窗口想要在新的窗口定位操作就需要切换窗口
原理获取窗口的唯一标识就是句柄获取到句柄就可以切换到对应的窗口了
处理方法
获取到当前的窗口柄(driver.current_window_handle)获取到所有的窗柄 (driver.window_handles)判断是否是想要操作的窗口如果是就可以对窗口进行操作如果不是跳转到另外一个窗口对另一个窗口进行操作(driver.switch_to.window)
示例百度搜索页和百度新闻页
class TestDemo():def setup(self):self.driver webdriver.Chrome()self.driver.maximize_window()self.driver.implicitly_wait(3) def test_baidu(self):self.driver.get(https://www.baidu.com/)search_window self.driver.current_window_handleself.driver.find_element_by_xpath(//*[ids-top-left]/a[1]).click()handles self.driver.window_handlesprint(handles)for handle in handles:if handle ! search_window:self.driver.switch_to.window(handle)print(self.driver.title)self.driver.find_element_by_id(ww).send_keys(autotest)self.driver.find_element_by_id(s_btn_wr).click()
2.处理frame
什么是frame?
frame是html中的框架在html中frame可以在同一个窗口中显示不止一个页面。
Frame分类 frame标签包含frameset、frame、iframe三种 frameset和普通的标签一样不会影响正常的定位可以使用index、id、name、webelement任意种方式定位frameset 而frame与iframe对selenium定位而言是一样的。selenium有一组方法对frame进行操作
注意一个页面中可能存在多个frame一个frame中可能嵌套frame 处理方法
跳转到frameswitch_to.frame(frame_reference) --frame_reference用于定位frame可以是idnameindex也可以是selenium获得WebElement对象。嵌套frame先进入父类frame再进入子frame才可以对子frame中的元素处理
driver.switch_to.frame(id) #id
driver.switch_to.frame(name) #name
driver.switch_to.frame(0) #index
driver.switch_to.frame(driver.find_element_by_xpath(path))#也可以用css等定位方式跳转出frame switch_to.default_content()切换到父级frame switch_to.parent_frame()
示例qq邮箱登录页面
def test_frame(self):self.driver.get(https://mail.qq.com/)self.driver.switch_to.frame(self.driver.find_element_by_xpath(//*[idQQMailSdkTool_login_loginBox_qq]/iframe))#切换到父类frameself.driver.switch_to.frame(ptlogin_iframe)#切换到frametime.sleep(3)self.driver.find_element_by_xpath(//*[idswitcher_plogin]).click()self.driver.switch_to.default_content()#跳出frameself.driver.find_element_by_class_name(header_logo).click()