Ejemplo n.º 1
0
def switch_to_new_window(driver):
    """Wait until new window will be opened, have the number of windows handles
  increase and then switch to last opened window.
  """
    try:
        wait_until_condition(driver, EC.new_window_is_opened)
        driver.switch_to.window(driver.window_handles.pop())
    except:
        raise exceptions.NoSuchWindowException(
            messages.ExceptionsMessages.err_switch_to_new_window)
Ejemplo n.º 2
0
    def switch_to_window(self, window_name=None, title=None, url=None):
        """
        WebDriver implements switching to other window only by it's name. With
        wrapper there is also option to switch by title of window or URL. URL
        can be also relative path.
        """
        if window_name:
            self.switch_to.window(window_name)
            return

        if url:
            url = self.get_url(path=url)

        for window_handle in self.window_handles:
            self.switch_to.window(window_handle)
            if title and self.title == title:
                return
            if url and self.current_url == url:
                return
        raise selenium_exc.NoSuchWindowException('Window (title=%s, url=%s) not found.' % (title, url))
Ejemplo n.º 3
0
def try_to_switch_to_window(vic_step, current_window_handle=None, print_=True):
    pause_time = 0
    _success = False
    _full_msg = ''
    msg = '无法切换到符合条件的窗口'
    msg_format = '经过{:.1f}秒 - {}'
    new_window_handle = None
    if not current_window_handle:
        try:
            current_window_handle = vic_step.dr.current_window_handle
        except exceptions.NoSuchWindowException:
            vic_step.logger.warning('【{}】\t无法获取当前窗口'.format(
                vic_step.execute_str),
                                    exc_info=True)
            current_window_handle = 'N/A'
    last_print_time = start_time = time.time()
    while (time.time() - start_time -
           pause_time) <= vic_step.timeout and not vic_step.force_stop:
        for window_handle in vic_step.dr.window_handles:
            if window_handle != current_window_handle:
                vic_step.dr.switch_to.window(window_handle)
                # 如果定位信息不全,且没有ui_data,则直接切换到这个窗口
                if (not vic_step.ui_by
                        or not vic_step.ui_locator) and not vic_step.ui_data:
                    new_window_handle = window_handle
                    msg = '切换到新窗口'
                    _success = True
                    break
                elif vic_step.ui_data:  # 如果定位信息不全,但提供了ui_data,则把ui_data视为窗口标题
                    vic_step.ui_by = 'xpath'
                    vic_step.ui_locator = '/html/head/title[contains(text(), "{}")]'.format(
                        vic_step.ui_data)
                    vic_step.variable_elements = None
                    vic_step.ui_base_element = None
                    _msg = '标题为{}的窗口'.format(vic_step.ui_data)
                else:  # 否则使用定位信息查找元素
                    _msg = '包含元素【By:{}|Locator:{}】的窗口'.format(
                        vic_step.ui_by, vic_step.ui_locator)

                run_result_temp, elements, _elapsed_time, _pause_time = wait_for_element_present(
                    vic_step, timeout=1, print_=False)
                pause_time += _pause_time
                if elements and ((not vic_step.ui_index and len(elements) == 1)
                                 or
                                 (vic_step.ui_index
                                  and vic_step.ui_index <= len(elements) - 1)):
                    new_window_handle = window_handle
                    msg = '切换到{}'.format(_msg)
                    _success = True
                    break
                else:
                    msg = '无法切换到{}'.format(_msg)

        if time.time() - last_print_time >= 1:
            pause_time += vic_step.pause_()
            elapsed_time = time.time() - start_time - pause_time
            _full_msg = msg_format.format(elapsed_time, msg)
            if print_:
                vic_step.logger.info('【{}】\t{}'.format(vic_step.execute_str,
                                                       _full_msg))
            last_print_time = time.time()

        if _success:
            break

    elapsed_time = time.time() - start_time - pause_time
    full_msg = msg_format.format(elapsed_time, msg)
    if print_ and full_msg != _full_msg:
        vic_step.logger.info('【{}】\t{}'.format(vic_step.execute_str, full_msg))
    if _success:
        run_result = ['p', full_msg]
    else:
        raise exceptions.NoSuchWindowException(full_msg)
    return run_result, new_window_handle, elapsed_time, pause_time