Python selenium.common.exceptions.NoSuchWindowException() Examples

The following are 11 code examples of selenium.common.exceptions.NoSuchWindowException(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module selenium.common.exceptions , or try the search function .
Example #1
Source File: request.py    From Selenium-Requests with MIT License 6 votes vote down vote up
def find_window_handle(webdriver, predicate):
    original_window_handle = webdriver.current_window_handle
    if predicate(webdriver):
        return original_window_handle

    # Start search beginning with the most recently added window handle: the chance is higher that this is the correct
    # one in most cases
    for window_handle in reversed(webdriver.window_handles):
        if window_handle == original_window_handle:
            continue

        # This exception can occur if the window handle was closed between accessing the window handles and attempting
        # to switch to it, in which case it can be silently ignored.
        try:
            webdriver.switch_to.window(window_handle)
        except NoSuchWindowException:
            continue

        if predicate(webdriver):
            return window_handle

    # Simply switch back to the original window handle and return None if no matching window handle was found
    webdriver.switch_to.window(original_window_handle) 
Example #2
Source File: collect.py    From autowebcompat with Mozilla Public License 2.0 5 votes vote down vote up
def close_all_windows_except_first(driver):
    windows = driver.window_handles

    for window in windows[1:]:
        driver.switch_to_window(window)
        driver.close()

    while True:
        try:
            alert = driver.switch_to_alert()
            alert.dismiss()
        except (NoAlertPresentException, NoSuchWindowException):
            break

    driver.switch_to_window(windows[0]) 
Example #3
Source File: crawler.py    From coverage-crawler with Mozilla Public License 2.0 5 votes vote down vote up
def close_all_windows_except_first(driver):
    windows = driver.window_handles

    for window in windows[1:]:
        driver.switch_to_window(window)
        driver.close()

    while True:
        try:
            alert = driver.switch_to_alert()
            alert.dismiss()
        except (NoAlertPresentException, NoSuchWindowException):
            break

    driver.switch_to_window(windows[0]) 
Example #4
Source File: funcselenium.py    From django-functest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def switch_window(self, handle=None):
        """
        Switches window.

        If there are only 2 windows, it can work out which window to switch to.
        Otherwise, you should pass in the window handle as `handle` kwarg.

        Returns a tuple (old_window_handle, new_window_handle)
        """
        try:
            current_window_handle = self._driver.current_window_handle
        except NoSuchWindowException:
            # Window closed recently
            current_window_handle = None
        window_handles = self._driver.window_handles
        if handle is None:
            possible_window_handles = [h for h in window_handles
                                       if h != current_window_handle]

            if len(possible_window_handles) > 1:
                raise AssertionError("Don't know which window to switch to!")
            else:
                handle = possible_window_handles[0]

        def f(driver):
            if (hasattr(driver, 'switch_to') and
                    hasattr(driver.switch_to, 'window')):
                m = driver.switch_to.window
            else:
                m = driver.switch_to_window
            m(handle)
            return driver.current_window_handle == handle
        self.wait_until(f)
        return current_window_handle, handle 
Example #5
Source File: funcselenium.py    From django-functest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _wait_until_finished(self):
        try:
            self.wait_for_page_load()
        except NoSuchWindowException:
            pass  # window can legitimately close e.g. for popups 
Example #6
Source File: api.py    From redtide with MIT License 5 votes vote down vote up
def current_tab_id(self):
        try:
            return self.driver.current_window_handle
        except selenium_exceptions.NoSuchWindowException:
            print('Failed to get current tab_id, likely close. Switch to tab 0')
            try:
                self.to_tab_by_index(0)
                return self.driver.current_window_handle
            except:
                print('Still failed to get current tab_id')
                raise
        except:
            print('Failed to get current tab_id for unexpected reason')
            raise 
Example #7
Source File: request.py    From Selenium-Requests with MIT License 5 votes vote down vote up
def make_match_domain_predicate(domain):
    def predicate(webdriver):
        try:
            return get_tld(webdriver.current_url) == domain
        # This exception can occur if the current window handle was closed
        except NoSuchWindowException:
            pass

    return predicate 
Example #8
Source File: window.py    From nerodia with MIT License 5 votes vote down vote up
def _matches(self, handle):
        try:
            orig = self.driver.current_window_handle
        except NoSuchWindowException:
            orig = None
        try:
            self.driver.switch_to.window(handle)

            if 'title' in self.selector:
                title_value = self.selector.get('title')
                driver_title = self.browser.title
                matches_title = re.search(title_value, driver_title) is not None
            else:
                matches_title = True

            if 'url' in self.selector:
                url_value = self.selector.get('url')
                driver_url = self.browser.url
                matches_url = re.search(url_value, driver_url) is not None
            else:
                matches_url = True

            return matches_title and matches_url
        except (NoSuchWindowException, WebDriverException):
            return False
        finally:
            current = self.driver.window_handles
            orig = orig if orig in current else current[0]
            self.driver.switch_to.window(orig) 
Example #9
Source File: after_hooks.py    From nerodia with MIT License 5 votes vote down vote up
def run(self):
        """ Runs after hooks """
        # We can't just rescue exception because Firefox automatically closes alert when
        # exception raised
        try:
            if self.after_hooks and not self.browser.alert.exists:
                for hook in self.after_hooks:
                    hook(self.browser)
        except NoSuchWindowException as e:
            nerodia.logger.info('Could not execute After Hooks because browser window was '
                                'closed {}'.format(e)) 
Example #10
Source File: funcselenium.py    From django-functest with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def click(self, css_selector=None, xpath=None,
              text=None, text_parent_id=None,
              wait_for_reload=False,
              wait_timeout=None,
              double=False, scroll=True, window_closes=False):
        """
        Clicks the button or control specified by the CSS selector
        or xpath
        """
        if window_closes:
            wait_for_reload = False
        if wait_for_reload:
            self._driver.execute_script("document.pageReloadedYetFlag='notyet';")

        elem = self._find_with_timeout(css_selector=css_selector,
                                       xpath=xpath,
                                       text=text,
                                       text_parent_id=text_parent_id,
                                       timeout=wait_timeout)
        if scroll:
            self._scroll_into_view(elem)
        elem.click()
        if double:
            try:
                elem.click()
            except StaleElementReferenceException:
                pass

        if wait_for_reload:
            def f(driver):
                obj = driver.execute_script("return document.pageReloadedYetFlag;")

                if obj is None or obj != "notyet":
                    return True
                return False
            try:
                WebDriverWait(self._driver, self.get_default_timeout()).until(f)
            except NoSuchWindowException:
                # legitimate sometimes e.g. when window closes
                pass
        if not window_closes:
            self._wait_until_finished() 
Example #11
Source File: bot.py    From csb with GNU Affero General Public License v3.0 0 votes vote down vote up
def openChrome(paydetailsO, itemdetsO, timeO, strictO, skipO, nextO, cdloc, capabilities, useProxy, PROXY):
    global driver, strict, password, reg, items, droptime, pDescr, paydetails, category, skipS, nextS
    chrome_options = webdriver.ChromeOptions()
    if useProxy:
        prx = Proxy()
        prx.proxy_type = ProxyType.MANUAL
        prx.http_proxy = PROXY
        prx.socks_proxy = PROXY
        prx.ssl_proxy = PROXY
        prx.add_to_capabilities(capabilities)
    else:
        prx = Proxy()
        prx.proxy_type = ProxyType.SYSTEM
        prx.add_to_capabilities(capabilities)

    chrome_options.binary_location = capabilities['chrome.binary']

    driver = webdriver.Chrome(cdloc, desired_capabilities=capabilities)
    openTab('https://www.google.com', driver)
    paydetails = paydetailsO
    reg = paydetailsO['Region']
    strict = strictO
    skipS = skipO
    nextS = nextO
    droptime = timeO
    items = []
    for x in itemdetsO:
        print(x[0],x[1],x[2],x[3])
        items.append({'selectedCategory': x[0], 'keywords': x[1].split(','), 'selectedColour': x[2], 'selectedSize': x[3]})
    returnTime()
    try:
        for it in items:
                searchItem(it)
        cart()
    except NoSuchWindowException:
        print('[!] Chrome window closed. Click GO! to re-start')
        return None