U
    g%                     @   s~   d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d	d
lmZ G dd dZdS )    )Optional)Union)NoSuchElementException)NoSuchFrameException)NoSuchWindowException)Alert)By)
WebElement   )Commandc                   @   s   e Zd ZddddZeedddZeedddZddd	d
Z	e
eeef ddddZdee ddddZddddZeddddZeddddZdS )SwitchToN)returnc                 C   s   dd l }||| _d S )Nr   )weakrefproxy_driver)selfZdriverr    r   G/tmp/pip-unpacked-wheel-3ysq0mf8/selenium/webdriver/remote/switch_to.py__init__    s    zSwitchTo.__init__c                 C   s   | j tjd S )zReturns the element with focus, or BODY if nothing has focus.

        :Usage:
            ::

                element = driver.switch_to.active_element
        value)r   executer   ZW3C_GET_ACTIVE_ELEMENTr   r   r   r   active_element%   s    	zSwitchTo.active_elementc                 C   s   t | j}|j}|S )zSwitches focus to an alert on the page.

        :Usage:
            ::

                alert = driver.switch_to.alert
        )r   r   text)r   alert_r   r   r   r   0   s    	
zSwitchTo.alertc                 C   s   | j tjddi dS )zSwitch focus to the default frame.

        :Usage:
            ::

                driver.switch_to.default_content()
        idN)r   r   r   SWITCH_TO_FRAMEr   r   r   r   default_content=   s    zSwitchTo.default_content)frame_referencer   c                 C   s   t |trvz| jtj|}W nV tk
rt   z| jtj|}W n, tk
rn } zt||W 5 d}~X Y nX Y nX | j	t
jd|i dS )a  Switches focus to the specified frame, by index, name, or
        webelement.

        :Args:
         - frame_reference: The name of the window to switch to, an integer representing the index,
                            or a webelement that is an (i)frame to switch to.

        :Usage:
            ::

                driver.switch_to.frame('frame_name')
                driver.switch_to.frame(1)
                driver.switch_to.frame(driver.find_elements(By.TAG_NAME, "iframe")[0])
        Nr   )
isinstancestrr   Zfind_elementr   ZIDr   NAMEr   r   r   r   )r   r   excr   r   r   frameG   s    
"zSwitchTo.frame)	type_hintr   c                 C   s*   | j tjd|id }| |d  dS )a  Switches to a new top-level browsing context.

        The type hint can be one of "tab" or "window". If not specified the
        browser will automatically select it.

        :Usage:
            ::

                driver.switch_to.new_window('tab')
        typer   handleN)r   r   r   Z
NEW_WINDOW_w3c_window)r   r%   r   r   r   r   
new_windowa   s    zSwitchTo.new_windowc                 C   s   | j tj dS )zSwitches focus to the parent context. If the current context is the
        top level browsing context, the context remains unchanged.

        :Usage:
            ::

                driver.switch_to.parent_frame()
        N)r   r   r   ZSWITCH_TO_PARENT_FRAMEr   r   r   r   parent_frameo   s    	zSwitchTo.parent_frame)window_namer   c                 C   s   |  | dS )zSwitches focus to the specified window.

        :Args:
         - window_name: The name or window handle of the window to switch to.

        :Usage:
            ::

                driver.switch_to.window('main')
        N)r(   )r   r+   r   r   r   windowz   s    zSwitchTo.windowc                    sz    fdd}z|| W n\ t k
rt    jj} jj}|D ](}||  jd}||kr< Y d S q<||  Y nX d S )Nc                    s    j tjd| i d S )Nr'   )r   r   r   ZSWITCH_TO_WINDOW)hr   r   r   send_handle   s    z)SwitchTo._w3c_window.<locals>.send_handlezreturn window.name)r   r   Zcurrent_window_handleZwindow_handlesZexecute_script)r   r+   r.   Zoriginal_handleZhandlesr'   Zcurrent_namer   r   r   r(      s    
zSwitchTo._w3c_window)N)__name__
__module____qualname__r   propertyr	   r   r   r   r   r   r!   intr$   r   r)   r*   r,   r(   r   r   r   r   r      s   

r   N)typingr   r   Zselenium.common.exceptionsr   r   r   Zselenium.webdriver.common.alertr   Zselenium.webdriver.common.byr   Z$selenium.webdriver.remote.webelementr	   commandr   r   r   r   r   r   <module>   s   