browser_mgmt.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:warriorframework 作者: warriorframework 项目源码 文件源码
def switch_tab(self, browser_instance=None, tab_number=None, browser_type="firefox"):
        """Switching to different tabs in a browser with unique tab_number"""
        status = True
        if browser_instance is None:
            browser_instance = self.current_browser

        if tab_number is not None:
            try:
                tab_number = int(tab_number)
            except:
                print_error("{0} is not a valid tab number".format(tab_number))
                status = False
            else:
                if tab_number > len(browser_instance.window_handles) or tab_number < 1:
                    print_error("{0} is not a valid tab number".format(tab_number))
                    status = False
                else:
                    tab_number -= 1
                    current_tab = 0
                    current_window_handle = browser_instance.current_window_handle
                    for i in range(0, len(browser_instance.window_handles)):
                        if browser_instance.window_handles[i] == current_window_handle:
                            current_tab = i
                            break
                    if tab_number != current_tab:
                        if current_tab < tab_number:
                            times = tab_number - current_tab
                        else:
                            times = len(browser_instance.window_handles) - current_tab
                            times += tab_number
                        if browser_type == "firefox":
                            action_chains = ActionChains(browser_instance)
                            action_chains.key_down(Keys.ALT)
                            for i in range(0, times):
                                action_chains.send_keys('`')
                            action_chains.perform()
                        else:
                            element = browser_instance.find_element_by_tag_name('body')
                            for i in range(0, times):
                                element.send_keys(Keys.LEFT_CONTROL, Keys.TAB)
                        browser_instance.switch_to.window(browser_instance.window_handles[tab_number])
        else:
            current_tab = 0
            current_window_handle = browser_instance.current_window_handle
            for i in range(0, len(browser_instance.window_handles)):
                if browser_instance.window_handles[i] == current_window_handle:
                    current_tab = i
            tab_number = current_tab + 1
            if tab_number >= len(browser_instance.window_handles):
                tab_number = 0
            if browser_type == "firefox":
                browser_instance.find_element_by_tag_name('body').send_keys(Keys.LEFT_ALT, '`')
            else:
                browser_instance.find_element_by_tag_name('body').send_keys(Keys.LEFT_CONTROL, Keys.TAB)
            browser_instance.switch_to.window(browser_instance.window_handles[tab_number])

        return status
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号