• (五)selenium 切换frame_窗口_执行js


    一、 Switch_to

    处理场景:处理JS弹框,切换frame窗口 切换浏览器窗口

    1.1、处理JS弹框

    JavaScript 简称JS,应用于HTML网页,通过操纵网页窗口及网页元素,从而实现动态效果。
    有:警告框alert、确认框confirm、提示框prompt

    相关方法

    alert = driver.switch_to_alert  # 切换焦点到弹框
    alert.accept()   # 对弹框点击确定按钮
    alert.dismiss()   # 对弹框点击取消
    alert.text    # 获取弹框文本内容
    alert.send_keys(keysToSend)   # 往弹框输入框输入内容
    
    

    1.2、切换frame窗口

    如果元素在html的frame 或 iframe中,则无法直接定位到元素。需要先切换到该frame中,再进行定位及其他操作

    self.driver.switch_to.frame(frame_reference=) # 参数为frame的id 或name
    self.driver.switch_to.frame(0);   # 按索引值切换frame
    self.driver.switch_to.parent_frame()   # 切换到上级frame
    self.driver.switch_to.default_content()   # 切换到默认内容
    

    1.3、切换浏览器窗口

    浏览器中有多个窗口,如果想切换到其他窗口操作,需要调用switch_to方法切换到目标窗口:

    driver.switch_to.window("window_name")  # 切换焦点到指定窗口
    driver.current_window_handle  # 获取当前窗口的  handle
    driver.window_handles  # 获取当前浏览器所有窗口的  handle
    
    
        handels = driver.window_handles;   
        for handele in handels:
            driver.switch_to.window(handele)
            if driver.title == "测试网页":
                break
    
      def test_001(self):
            self.driver.get("https://www.baidu.com/")
            self.driver.find_element_by_id("kw").send_keys("selenium")
            self.driver.find_element_by_id("su").click()
            # self.driver.find_element_by_link_text("Selenium_百度百科").click()
            self.driver.find_element_by_partial_link_text("百度百科").click()
    
            #获取当前窗口handle
            currenthandl=self.driver.current_window_handle
            # 获取全部的窗口handles
            handl_list=self.driver.window_handles
            # 切换到第二个窗口
            self.driver.switch_to.window(handl_list[1])
            self.driver.find_element_by_id("query").send_keys("自动化测试")
    

    思考:当你不知道要操作的元素 在那个窗口 怎么设计操作浏览器中有多个窗口,如果想切换到其他窗口操作,需要调用switch_to方法切换到目标窗口:

    driver.switch_to.window("window_name")  # 切换焦点到指定窗口
    driver.current_window_handle  # 获取当前窗口的  handle
    driver.window_handles  # 获取当前浏览器所有窗口的  handle
    
    
        handels = driver.window_handles;   
        for handele in handels:
            driver.switch_to.window(handele)
            if driver.title == "测试网页":
                break
    
      def test_001(self):
            self.driver.get("https://www.baidu.com/")
            self.driver.find_element_by_id("kw").send_keys("selenium")
            self.driver.find_element_by_id("su").click()
            # self.driver.find_element_by_link_text("Selenium_百度百科").click()
            self.driver.find_element_by_partial_link_text("百度百科").click()
    
            #获取当前窗口handle
            currenthandl=self.driver.current_window_handle
            # 获取全部的窗口handles
            handl_list=self.driver.window_handles
            # 切换到第二个窗口
            self.driver.switch_to.window(handl_list[1])
            self.driver.find_element_by_id("query").send_keys("自动化测试")
    

    思考:当你不知道要操作的元素 在那个窗口 怎么设计操作

    二、鼠标键盘操作模拟

    一般来说我们与页面的交互可以使用WebElement的方法进行点击等操作,但是有时候我们需要一些更复杂的动作,就需要用到我们的Action Chains了

    通过ActionChains 对象模拟鼠标复杂操作:

    from selenium.webdriver.common.action_chains import ActionChains
    
    
            actions = ActionChains(self.driver);
            actions.click(on_element)  # 鼠标左键单击指定的元素e
            actions.click()   # 鼠标左键点击当前元素
            actions.context_click()  #鼠标右键单击
            actions.double_click(on_element)  # 鼠标双击
            actions.click_and_hold(on_element)  # 鼠标点住不放
            actions.move_to_element(to_element)  # 鼠标悬停
            actions.drag_and_drop(source,target)  # 鼠标拖拽元素到某元素
            actions.drag_and_drop_by_offset(scource,xoffset,yoffset)  # 拖拽元素到某位置, 左右:x,上下:y,下右为正
            
            actions.perform()    # 执行存储在任务列表中的操作
            actions.reset_actions()  # 清除储存在任务列表中的操作
            
    

    ActionChains 模拟键盘操作

    普通键:
    ● 无名按键:a-z,0-9
    ● 有名按键:Tab、Enter、Space(空格键)、BackSpace(退格键)
    修饰键:修饰键单独使用没有意义,一般和别的按键配合使用
    Alt、Shift、Control

    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    
    
    Actions操作:
    action = ActionChains(driver);
    
    # 使用示例
    action = ActionChains(self.driver)
    
    action.send_keys(Keys.ENTER)  #  对当前元素 按回车键
    action.send_keys_to_element(element, *keys_to_send)  # 对指定元素 做指定操作
    action.key_down(Keys.CONTROL).send_keys("a")  # ctrl +a 操作
    action.key_down(Keys.SHIFT).send_keys("abc")  # 输入大写 abc
    

    二、js代码执行

    webdriver 无法完成的操作,可以由JS配合完成

    self.driver.execute_script("return document.title"); # 获取title
    self.driver.execute_script("arguments[0].setAttribute('style',arguments[1]);",element,"color:orange;border:4px solid orange;") # 设置橘色边框
    
    参考JS:
        弹出提示框:alert('我是一个弹框')
        改变元素熟悉: q = document.getElementById('kw')
        			  q.style.display='block';
            
    self.driver.execute_async_script()  # 异步请求:客户端请求服务端,会先回复收到,不会立即返回结果
    

    文件上传下载

        self.driver.get("http://106.14.225.213/register")
         self.driver.find_element_by_class_name("avatar").click()
         self.driver.find_element_by_xpath("//*[@name='file']").send_keys(r"C:\Users\huangwei\Desktop\test\test_img.jpg")
         self.setup(3)
    
  • 相关阅读:
    ImageLightbox.js – 响应式的图片 Lightbox 插件
    精美素材:10套最新出炉的免费扁平图标下载
    盘点2013年那些最优秀的网页设计作品【系列三】
    12个带给你惊喜用户体验的手机界面设计
    一款效果精致的 jQuery 多层滑出菜单插件
    创意无限:20幅惊人的霓虹灯摄影照片欣赏
    『设计前沿』14款精致的国外 iOS7 图标设计示例
    2013年值得我们学习的网页设计作品【系列二】
    Summernote – 基于 Bootstrap 的文本编辑器
    『摄影欣赏』2013年微软必应搜索十大首页美图
  • 原文地址:https://www.cnblogs.com/niunai/p/15125362.html
Copyright © 2020-2023  润新知