(service: Service, *, session: requests.Session)
:raises ImportError: of Selenium
    :raises WebDriverException:
:raises ImportError: of Selenium
    :raises WebDriverException:
: raises ImportError : of Selenium : raises WebDriverException :
def login_with_browser(service: Service, *, session: requests.Session) -> None:
    """
    :raises ImportError: of Selenium
    :raises WebDriverException:
    """

    import selenium.webdriver  # pylint: disable=import-error,import-outside-toplevel

    with get_webdriver() as driver:
        # get cookies via Selenium
        url = service.get_url_of_login_page()
        logger.info('Opening the URL via WebDriver: %s', url)
        logger.info('Please do the followings:\n    1. login in the GUI browser\n    2. close the GUI browser')
        driver.get(url)
        cookies: List[Dict[str, str]] = []
        try:
            while driver.current_url:
                cookies = driver.get_cookies()
                time.sleep(0.1)
        except selenium.common.exceptions.WebDriverException as e:
            logger.debug(e)  # the window is closed

    # set cookies to the requests.Session
    logger.info('Copying cookies via WebDriver...')
    for c in cookies:
        logger.debug('set cookie: %s', c['name'])
        morsel: http.cookies.Morsel = http.cookies.Morsel()
        morsel.set(c['name'], c['value'], c['value'])
        morsel.update({key: value for key, value in c.items() if morsel.isReservedKey(key)})
        if not morsel['expires']:
            expires = datetime.datetime.now(datetime.timezone.utc).astimezone() + datetime.timedelta(days=180)
            morsel.update({'expires': expires.strftime('%a, %d-%b-%Y %H:%M:%S GMT')})  # RFC2109 format
        cookie = requests.cookies.morsel_to_cookie(morsel)
        session.cookies.set_cookie(cookie)
def login_with_browser ( service : Service , * , session : requests . Session ) -> None : import selenium . webdriver # pylint: disable=import-error,import-outside-toplevel with get_webdriver ( ) as driver : # get cookies via Selenium url = service . get_url_of_login_page ( ) logger . info ( 'Opening the URL via WebDriver: %s' , url ) logger . info ( 'Please do the followings:\n    1. login in the GUI browser\n    2. close the GUI browser' ) driver . get ( url ) cookies : List [ Dict [ str , str ] ] = [ ] try : while driver . current_url : cookies = driver . get_cookies ( ) time . sleep ( 0.1 ) except selenium . common . exceptions . WebDriverException as e : logger . debug ( e ) # the window is closed # set cookies to the requests.Session logger . info ( 'Copying cookies via WebDriver...' ) for c in cookies : logger . debug ( 'set cookie: %s' , c [ 'name' ] ) morsel : http . cookies . Morsel = http . cookies . Morsel ( ) morsel . set ( c [ 'name' ] , c [ 'value' ] , c [ 'value' ] ) morsel . update ( { key : value for key , value in c . items ( ) if morsel . isReservedKey ( key ) } ) if not morsel [ 'expires' ] : expires = datetime . datetime . now ( datetime . timezone . utc ) . astimezone ( ) + datetime . timedelta ( days = 180 ) morsel . update ( { 'expires' : expires . strftime ( '%a, %d-%b-%Y %H:%M:%S GMT' ) } ) # RFC2109 format cookie = requests . cookies . morsel_to_cookie ( morsel ) session . cookies . set_cookie ( cookie )