
    ,i                        d dl Z d dlmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZ  G d d      Z G d d	      Z G d
 d      Z G d d      Z G d de      Z G d d      Z G d d      Z G d d      Z G d de      Z G d de      Z G d d      Z G d d      Z G d d      Z G d  d!      Z G d" d#      Z G d$ d%      Z G d& d'      Z G d( d)      Z G d* d+      Z G d, d-      Z G d. d/      Z  G d0 d1      Z! G d2 d3      Z" G d4 d5      Z# G d6 d7      Z$e G d8 d9             Z% G d: d;      Z& G d< d=      Z'y)>    N)	dataclass)AnyCallableOptionalUnion)command_builder)Sessionc                       e Zd ZdZdZdZdZy)ReadinessStatezSRepresents the stage of document loading at which a navigation command will return.noneinteractivecompleteN)__name__
__module____qualname____doc__NONEINTERACTIVECOMPLETE     ~/var/www/system.tvplus.app.br/jogos/scrap/venv/lib/python3.12/site-packages/selenium/webdriver/common/bidi/browsing_context.pyr   r      s    ]DKHr   r   c                        e Zd ZdZdZdZdZdZy)UserPromptTypez*Represents the possible user prompt types.alertbeforeunloadconfirmpromptN)r   r   r   r   ALERTBEFORE_UNLOADCONFIRMPROMPTr   r   r   r   r   "   s    4E"MGFr   r   c                   H    e Zd ZdZdedee   dedefdZede	dd fd	       Z
y
)NavigationInfoz*Provides details of an ongoing navigation.context
navigation	timestampurlc                 <    || _         || _        || _        || _        y N)r%   r&   r'   r(   )selfr%   r&   r'   r(   s        r   __init__zNavigationInfo.__init__.   s      $"r   jsonreturnc                    |j                  d      }|t        |t              st        d      |j                  d      }|t        |t              st        d      |j                  d      }|t        |t              r|dk  rt        d      |j                  d      }|t        |t              st        d	       | ||||      S )
a  Creates a NavigationInfo instance from a dictionary.

        Parameters:
        -----------
            json: A dictionary containing the navigation information.

        Returns:
        -------
            NavigationInfo: A new instance of NavigationInfo.
        r%   (context is required and must be a stringr&   znavigation must be a stringr'   r   8timestamp is required and must be a non-negative integerr(   $url is required and must be a stringget
isinstancestr
ValueErrorint)clsr-   r%   r&   r'   r(   s         r   	from_jsonzNavigationInfo.from_json:   s     ((9%?*Wc":GHHXXl+
!*Z*E:;;HH[)	Jy#$>)a-WXXhhuo;jc2CDD7J	377r   N)r   r   r   r   r6   r   r8   r,   classmethoddictr:   r   r   r   r$   r$   +   sW    4

 SM
 	

 
 8T 8&6 8 8r   r$   c                   l    e Zd ZdZ	 	 ddededeed       dededee   d	ee   fd
Zede	dd fd       Z
y)BrowsingContextInfoz)Represents the properties of a navigable.Nr%   r(   childrenclient_windowuser_contextparentoriginal_openerc                 f    || _         || _        || _        || _        || _        || _        || _        y r*   )r%   r(   r?   rB   rA   rC   r@   )r+   r%   r(   r?   r@   rA   rB   rC   s           r   r,   zBrowsingContextInfo.__init__\   s8      (.*r   r-   r.   c           	      P   d}|j                  d      }|ot        |t              st        d      g }|D ]M  }t        |t              st        dt        |             |j                  t        j                  |             O |j                  d      }|t        |t              st        d      |j                  d      }|t        |t              st        d      |j                  d	      }|t        |t              st        d
      |j                  d      }|t        |t              st        d      |j                  d      }	|	t        |	t              st        d      |j                  d      }
|
t        |
t              st        d       | ||||
|||	      S )a!  Creates a BrowsingContextInfo instance from a dictionary.

        Parameters:
        -----------
            json: A dictionary containing the browsing context information.

        Returns:
        -------
            BrowsingContextInfo: A new instance of BrowsingContextInfo.
        Nr?   z#children must be a list if providedz%Each child must be a dictionary, got r%   r0   r(   r2   rB   z#parent must be a string if provideduserContextz,userContext is required and must be a stringoriginalOpenerz+originalOpener must be a string if providedclientWindowz-clientWindow is required and must be a string)r%   r(   r?   r@   rA   rB   rC   )
r4   r5   listr7   r<   typeappendr>   r:   r6   )r9   r-   r?   raw_childrenchildr%   r(   rB   rA   rC   r@   s              r   r:   zBrowsingContextInfo.from_jsonn   s    xx
+#lD1 !FGGH% F!%.$'LTRW[M%Z[[ 3 = =e DEF
 ((9%?*Wc":GHHhhuo;jc2CDD(#j&=BCCxx.z,'DKLL((#34&z/3/OJKK0 
=#(FLMM'%+
 	
r   NN)r   r   r   r   r6   r   rI   r,   r;   r<   r:   r   r   r   r>   r>   Y   s    3 !%)-++ + 4 567	+
 + + + "#+$ 7
T 7
&; 7
 7
r   r>   c            
       X     e Zd ZdZdedee   dededef
 fdZede	d	d fd
       Z
 xZS )DownloadWillBeginParamsz+Parameters for the downloadWillBegin event.r%   r&   r'   r(   suggested_filenamec                 8    t         |   ||||       || _        y r*   )superr,   rQ   )r+   r%   r&   r'   r(   rQ   	__class__s         r   r,   z DownloadWillBeginParams.__init__   s      	*i="4r   r-   r.   c                     t         j                  |      }|j                  d      }|t        |t              st        d       | |j                  |j                  |j                  |j                  |      S )NsuggestedFilenamez2suggestedFilename is required and must be a string)r%   r&   r'   r(   rQ   )
r$   r:   r4   r5   r6   r7   r%   r&   r'   r(   )r9   r-   nav_inforQ   s       r   r:   z!DownloadWillBeginParams.from_json   sp    !++D1!XX&9:%Z8JC-PQRR$$**((1
 	
r   )r   r   r   r   r6   r   r8   r,   r;   r<   r:   __classcell__rT   s   @r   rP   rP      sb    5	5	5 SM	5 		5
 	5  	5 
T 
&? 
 
r   rP   c                   P    e Zd ZdZ	 ddededededee   f
dZed	ed
d fd       Z	y)UserPromptOpenedParamsz*Parameters for the userPromptOpened event.Nr%   handlermessagerJ   default_valuec                 J    || _         || _        || _        || _        || _        y r*   r%   r\   r]   rJ   r^   )r+   r%   r\   r]   rJ   r^   s         r   r,   zUserPromptOpenedParams.__init__   s(     	*r   r-   r.   c                    |j                  d      }|t        |t              st        d      |j                  d      }|t        |t              st        d      |j                  d      }|t        |t              st        d      |j                  d      }|t        |t              st        d      |j                  d	      }|t        |t              st        d
       | |||||      S )a$  Creates a UserPromptOpenedParams instance from a dictionary.

        Parameters:
        -----------
            json: A dictionary containing the user prompt parameters.

        Returns:
        -------
            UserPromptOpenedParams: A new instance of UserPromptOpenedParams.
        r%   r0   r\   z(handler is required and must be a stringr]   z(message is required and must be a stringrJ   %type is required and must be a stringdefaultValuez)defaultValue must be a string if providedr`   )r4   r5   r6   r7   )r9   r-   r%   r\   r]   
type_valuer^   s          r   r:   z UserPromptOpenedParams.from_json   s     ((9%?*Wc":GHH((9%?*Wc":GHH((9%?*Wc":GHHXXf%
Z
C%@DEE0$Zs-KHII'
 	
r   r*   )
r   r   r   r   r6   r   r,   r;   r<   r:   r   r   r   r[   r[      sf    4 (,++ + 	+
 +  }+ %
T %
&> %
 %
r   r[   c            
       L    e Zd ZdZ	 ddedededee   fdZede	d	d fd
       Z
y)UserPromptClosedParamsz*Parameters for the userPromptClosed event.Nr%   acceptedrJ   	user_textc                 <    || _         || _        || _        || _        y r*   r%   rg   rJ   rh   )r+   r%   rg   rJ   rh   s        r   r,   zUserPromptClosedParams.__init__  s       	"r   r-   r.   c                    |j                  d      }|t        |t              st        d      |j                  d      }|t        |t              st        d      |j                  d      }|t        |t              st        d      |j                  d      }|t        |t              st        d       | ||||	      S )
a+  Creates a UserPromptClosedParams instance from a dictionary.

        Parameters:
        -----------
            json: A dictionary containing the user prompt closed parameters.

        Returns:
        -------
            UserPromptClosedParams: A new instance of UserPromptClosedParams.
        r%   r0   rg   z*accepted is required and must be a booleanrJ   rb   userTextz%userText must be a string if providedrj   )r4   r5   r6   r7   bool)r9   r-   r%   rg   rd   rh   s         r   r:   z UserPromptClosedParams.from_json  s     ((9%?*Wc":GHH88J':h#=IJJXXf%
Z
C%@DEEHHZ(	 Is)CDEE	
 	
r   r*   )r   r   r   r   r6   rm   r   r,   r;   r<   r:   r   r   r   rf   rf     s\    4 $(
#
# 
# 	
#
 C=
#  
T  
&>  
  
r   rf   c                   >    e Zd ZdZdededefdZededd fd       Z	y	)
HistoryUpdatedParamsz(Parameters for the historyUpdated event.r%   r'   r(   c                 .    || _         || _        || _        y r*   r%   r'   r(   )r+   r%   r'   r(   s       r   r,   zHistoryUpdatedParams.__init__8  s     "r   r-   r.   c                 6   |j                  d      }|t        |t              st        d      |j                  d      }|t        |t              r|dk  rt        d      |j                  d      }|t        |t              st        d       | |||      S )	a"  Creates a HistoryUpdatedParams instance from a dictionary.

        Parameters:
        -----------
            json: A dictionary containing the history updated parameters.

        Returns:
        -------
            HistoryUpdatedParams: A new instance of HistoryUpdatedParams.
        r%   r0   r'   r   r1   r(   r2   rq   r3   )r9   r-   r%   r'   r(   s        r   r:   zHistoryUpdatedParams.from_jsonB  s     ((9%?*Wc":GHHHH[)	Jy#$>)a-WXXhhuo;jc2CDD
 	
r   N)
r   r   r   r   r6   r8   r,   r;   r<   r:   r   r   r   ro   ro   5  sI    2  	 
T 
&< 
 
r   ro   c                   X     e Zd Z	 d
dedee   dededef
 fdZededd fd	       Z	 xZ
S )DownloadCanceledParamsr%   r&   r'   r(   statusc                 8    t         |   ||||       || _        y r*   )rS   r,   ru   )r+   r%   r&   r'   r(   ru   rT   s         r   r,   zDownloadCanceledParams.__init__b  s     	*i=r   r-   r.   c                     t         j                  |      }|j                  d      }||dk7  rt        d       | |j                  |j
                  |j                  |j                  |      S )Nru   canceledz)status is required and must be 'canceled')r%   r&   r'   r(   ru   )r$   r:   r4   r7   r%   r&   r'   r(   )r9   r-   rW   ru   s       r   r:   z DownloadCanceledParams.from_jsonm  sj    !++D1(#>Vz1HII$$**((
 	
r   )rx   r   r   r   r6   r   r8   r,   r;   r<   r:   rX   rY   s   @r   rt   rt   a  sd     !		 SM	 		
 	 	 
T 
&> 
 
r   rt   c                   d     e Zd Z	 	 ddedee   dedededee   f fdZeded	d fd
       Z	 xZ
S )DownloadCompleteParamsr%   r&   r'   r(   ru   filepathc                 F    t         |   ||||       || _        || _        y r*   )rS   r,   ru   r|   )r+   r%   r&   r'   r(   ru   r|   rT   s          r   r,   zDownloadCompleteParams.__init__  s&     	*i= r   r-   r.   c                 :   t         j                  |      }|j                  d      }||dk7  rt        d      |j                  d      }|t	        |t
              st        d       | |j                  |j                  |j                  |j                  ||      S )Nru   r   z)status is required and must be 'complete'r|   z%filepath must be a string if provided)r%   r&   r'   r(   ru   r|   )
r$   r:   r4   r7   r5   r6   r%   r&   r'   r(   )r9   r-   rW   ru   r|   s        r   r:   z DownloadCompleteParams.from_json  s    !++D1(#>Vz1HII88J'
8S(ADEE$$**((
 	
r   )r   Nry   rY   s   @r   r{   r{   ~  su     !"&!! SM! 	!
 ! ! 3-! 
T 
&> 
 
r   r{   c                   @    e Zd ZdZdeeef   fdZede	dd fd       Z
y)DownloadEndParamsz%Parameters for the downloadEnd event.download_paramsc                     || _         y r*   )r   )r+   r   s     r   r,   zDownloadEndParams.__init__  s      /r   r-   r.   c                     |j                  d      }|dk(  r | t        j                  |            S |dk(  r | t        j                  |            S t	        d      )Nru   rx   r   z.status must be either 'canceled' or 'complete')r4   rt   r:   r{   r7   )r9   r-   ru   s      r   r:   zDownloadEndParams.from_json  sZ    (#Z-77=>>z!-77=>>MNNr   N)r   r   r   r   r   rt   r{   r,   r;   r<   r:   r   r   r   r   r     sE    //57MMN/ OT O&9 O Or   r   c                   *    e Zd ZdZdZedefd       Zy)ContextCreatedz5Event class for browsingContext.contextCreated event.browsingContext.contextCreatedr-   c                 P    t        |t              r|S t        j                  |      S r*   r5   r>   r:   r9   r-   s     r   r:   zContextCreated.from_json  #    d/0K",,T22r   Nr   r   r   r   event_classr;   r<   r:   r   r   r   r   r     s#    ?2K3T 3 3r   r   c                   *    e Zd ZdZdZedefd       Zy)ContextDestroyedz7Event class for browsingContext.contextDestroyed event. browsingContext.contextDestroyedr-   c                 P    t        |t              r|S t        j                  |      S r*   r   r   s     r   r:   zContextDestroyed.from_json  r   r   Nr   r   r   r   r   r     s#    A4K3T 3 3r   r   c                   *    e Zd ZdZdZedefd       Zy)NavigationStartedz8Event class for browsingContext.navigationStarted event.!browsingContext.navigationStartedr-   c                 P    t        |t              r|S t        j                  |      S r*   r5   r$   r:   r   s     r   r:   zNavigationStarted.from_json  "    dN+K''--r   Nr   r   r   r   r   r     #    B5K.T . .r   r   c                   *    e Zd ZdZdZedefd       Zy)NavigationCommittedz:Event class for browsingContext.navigationCommitted event.#browsingContext.navigationCommittedr-   c                 P    t        |t              r|S t        j                  |      S r*   r   r   s     r   r:   zNavigationCommitted.from_json  r   r   Nr   r   r   r   r   r     s#    D7K.T . .r   r   c                   *    e Zd ZdZdZedefd       Zy)NavigationFailedz7Event class for browsingContext.navigationFailed event. browsingContext.navigationFailedr-   c                 P    t        |t              r|S t        j                  |      S r*   r   r   s     r   r:   zNavigationFailed.from_json  r   r   Nr   r   r   r   r   r     #    A4K.T . .r   r   c                   *    e Zd ZdZdZedefd       Zy)NavigationAbortedz8Event class for browsingContext.navigationAborted event.!browsingContext.navigationAbortedr-   c                 P    t        |t              r|S t        j                  |      S r*   r   r   s     r   r:   zNavigationAborted.from_json  r   r   Nr   r   r   r   r   r     r   r   r   c                   *    e Zd ZdZdZedefd       Zy)DomContentLoadedz7Event class for browsingContext.domContentLoaded event. browsingContext.domContentLoadedr-   c                 P    t        |t              r|S t        j                  |      S r*   r   r   s     r   r:   zDomContentLoaded.from_json  r   r   Nr   r   r   r   r   r     r   r   r   c                   *    e Zd ZdZdZedefd       Zy)Loadz+Event class for browsingContext.load event.browsingContext.loadr-   c                 P    t        |t              r|S t        j                  |      S r*   r   r   s     r   r:   zLoad.from_json  r   r   Nr   r   r   r   r   r   
  s#    5(K.T . .r   r   c                   *    e Zd ZdZdZedefd       Zy)FragmentNavigatedz8Event class for browsingContext.fragmentNavigated event.!browsingContext.fragmentNavigatedr-   c                 P    t        |t              r|S t        j                  |      S r*   r   r   s     r   r:   zFragmentNavigated.from_json  r   r   Nr   r   r   r   r   r     r   r   r   c                   *    e Zd ZdZdZedefd       Zy)DownloadWillBeginz8Event class for browsingContext.downloadWillBegin event.!browsingContext.downloadWillBeginr-   c                 ,    t         j                  |      S r*   )rP   r:   r   s     r   r:   zDownloadWillBegin.from_json'  s    &0066r   Nr   r   r   r   r   r   "  s#    B5K7T 7 7r   r   c                   *    e Zd ZdZdZedefd       Zy)UserPromptOpenedz7Event class for browsingContext.userPromptOpened event. browsingContext.userPromptOpenedr-   c                 ,    t         j                  |      S r*   )r[   r:   r   s     r   r:   zUserPromptOpened.from_json1      %//55r   Nr   r   r   r   r   r   ,  #    A4K6T 6 6r   r   c                   *    e Zd ZdZdZedefd       Zy)UserPromptClosedz7Event class for browsingContext.userPromptClosed event. browsingContext.userPromptClosedr-   c                 ,    t         j                  |      S r*   )rf   r:   r   s     r   r:   zUserPromptClosed.from_json;  r   r   Nr   r   r   r   r   r   6  r   r   r   c                   *    e Zd ZdZdZedefd       Zy)HistoryUpdatedz5Event class for browsingContext.historyUpdated event.browsingContext.historyUpdatedr-   c                 ,    t         j                  |      S r*   )ro   r:   r   s     r   r:   zHistoryUpdated.from_jsonE  s    #--d33r   Nr   r   r   r   r   r   @  s#    ?2K4T 4 4r   r   c                   *    e Zd ZdZdZedefd       Zy)DownloadEndz2Event class for browsingContext.downloadEnd event.browsingContext.downloadEndr-   c                 ,    t         j                  |      S r*   )r   r:   r   s     r   r:   zDownloadEnd.from_jsonO  s     **400r   Nr   r   r   r   r   r   J  s#    </K1T 1 1r   r   c                   ,    e Zd ZU eed<   eed<   eed<   y)EventConfig	event_key
bidi_eventr   N)r   r   r   r6   __annotations__rJ   r   r   r   r   r   T  s    NOr   r   c            	           e Zd ZdZdeeef   fdZdedefdZdded	e	e
e      ddfd
ZdeddfdZdededdfdZdededdfdZddeded	e	e
e      defdZdededdfdZddZy)_EventManagerzFClass to manage event subscriptions and callbacks for BrowsingContext.event_configsc                 4   || _         || _        i | _        |j                         D ci c]  }|j                  |j
                   c}| _        dj                  t        |j                                     | _
        t        j                         | _        y c c}w )Nz, )connr   subscriptionsvaluesr   r   _bidi_to_classjoinsortedkeys_available_events	threadingLock_subscription_lock)r+   r   r   configs       r   r,   z_EventManager.__init__^  sz    	*#%S`SgSgSijv00&2D2DDj!%6-2D2D2F+G!H"+.."2 ks   Beventr.   c                 v    | j                   j                  |      }|st        d| d| j                         |S )NzEvent 'z' not found. Available events: )r   r4   r7   r   )r+   r   event_configs      r   validate_eventz_EventManager.validate_eventg  sA    ))--e4wug-LTMcMcLdeffr   Nr   contextsc                    | j                   5  || j                  vrPt        | j                        }| j                  j	                  |j                  ||             g | j                  |<   ddd       y# 1 sw Y   yxY w)zSubscribe to a BiDi event if not already subscribed.

        Parameters:
        ----------
            bidi_event: The BiDi event name.
            contexts: Optional browsing context IDs to subscribe to.
        )browsing_contextsN)r   r   r	   r   execute	subscribe)r+   r   r   sessions       r   subscribe_to_eventz _EventManager.subscribe_to_eventm  sq     $$ 	4!3!33!$)),		!!'"3"3JRZ"3"[\13"":.		4 	4 	4s   AA55A>c                    | j                   5  | j                  j                  |      }|N|sLt        | j                        }| j                  j                  |j                  |             | j                  |= ddd       y# 1 sw Y   yxY w)zUnsubscribe from a BiDi event if no more callbacks exist.

        Parameters:
        ----------
            bidi_event: The BiDi event name.
        N)r   r   r4   r	   r   r   unsubscribe)r+   r   callback_listr   s       r   unsubscribe_from_eventz$_EventManager.unsubscribe_from_event{  sy     $$ 	3 ..22:>M(!$)),		!!'"5"5j"AB&&z2	3 	3 	3s   A,BBcallback_idc                     | j                   5  | j                  |   j                  |       d d d        y # 1 sw Y   y xY wr*   )r   r   rK   )r+   r   r   s      r   add_callback_to_trackingz&_EventManager.add_callback_to_tracking  s9    $$ 	?z*11+>	? 	? 	?s   5>c                     | j                   5  | j                  j                  |      }|r||v r|j                  |       d d d        y # 1 sw Y   y xY wr*   )r   r   r4   remove)r+   r   r   r   s       r   remove_callback_from_trackingz+_EventManager.remove_callback_from_tracking  sN    $$ 	2 ..22:>M!=$$[1	2 	2 	2s   3A		Acallbackc                     | j                  |      }| j                  j                  |j                  |      }| j	                  |j
                  |       | j                  |j
                  |       |S r*   )r   r   add_callbackr   r   r   r   )r+   r   r   r   r   r   s         r   add_event_handlerz_EventManager.add_event_handler  sd    **51ii,,\-E-ExP 	 7 7B 	%%l&=&={Kr   c                     | j                  |      }| j                  j                  |j                  |       | j	                  |j
                  |       | j                  |j
                         y r*   )r   r   remove_callbackr   r   r   r   )r+   r   r   r   s       r   remove_event_handlerz"_EventManager.remove_event_handler  s\    **51 			!!,":":KH 	**<+B+BKP 	##L$;$;<r   c                    | j                   5  | j                  s
	 ddd       yt        | j                        }t	        | j                  j                               D ]p  \  }}| j                  j                  |      }|s$|D ]  }| j                  j                  ||         | j                  j                  |j                  |             r | j                  j                          ddd       y# 1 sw Y   yxY wz3Clear all event handlers from the browsing context.N)r   r   r	   r   rI   itemsr   r4   r   r   r   clear)r+   r   r   callback_idsr   r   s         r   clear_event_handlersz"_EventManager.clear_event_handlers  s    $$ 	'%%	' 	' dii(G,01C1C1I1I1K,L G(
L"1155jA'3 L		11+{KL II%%g&9&9*&EFG $$&	' 	' 	's   C2AC2 A)C22C;r*   r.   N)r   r   r   r   r<   r6   r   r,   r   r   rI   r   r   r8   r   r   r   r   r   r   r   r   r   r   r   [  s    P3Dk1A,B 3C K 4S 4HT#Y<O 4[_ 43 3 3?3 ?S ?T ?2 2# 2RV 2s h (SWX[S\J] il 
=# 
=C 
=D 
='r   r   c                      e Zd ZdZ edde       edde       edde       edd	e       ed
de	       edde
       edde       edde       edde       edde       edde       edde       edde       edde      dZd Zed ee   fd!       Zd"ed d#fd$Z	 	 	 dTd"ed&ed'ee   d(ee   d ef
d)ZdUd"ed*ed d#fd+Z	 	 	 dVd,ed-ee   d.ed/ee   d ef
d0Z	 	 dWd1ee    d2ee   d ee!   fd3Z"	 	 dWd"ed4ee   d5ee   d d#fd6Z#	 	 	 dXd"ed7ed8ee    d9ee   d:eee      d ee   fd;Z$	 dYd"ed<ed=ee   d efd>Z%	 	 	 	 	 	 	 dZd"ed.ed?ee   d@edAee   dBeee&e ef         dCe'dDed efdEZ(	 	 dWd"edFee   d=ee   d efdGZ)	 	 	 	 d[d"ee   d%ee   dHee'   dIeee      d d#f
dJZ*d"edKe d efdLZ+dYdMedNe,dOeee      d e fdPZ-dMedQe d d#fdRZ.d\dSZ/y#)]BrowsingContextz2BiDi implementation of the browsingContext module.context_createdr   context_destroyedr   dom_content_loadedr   download_endr   download_will_beginr   fragment_navigatedr   history_updatedr   loadr   navigation_abortedr   navigation_committedr   navigation_failedr   navigation_startedr   user_prompt_closedr   user_prompt_openedr   )r  r  r  r  r  r  r  r  r	  r
  r  r  r  r  c                 H    || _         t        || j                        | _        y r*   )r   r   EVENT_CONFIGS_event_manager)r+   r   s     r   r,   zBrowsingContext.__init__  s    	+D$2D2DEr   r.   c                 H    t        | j                  j                               S )zGet a list of all available event names.

        Returns:
        -------
            List[str]: A list of event names that can be used with event handlers.
        )rI   r  r   )r9   s    r   get_event_nameszBrowsingContext.get_event_names  s     C%%**,--r   r%   Nc                 V    d|i}| j                   j                  t        d|             y)a  Activates and focuses the given top-level traversable.

        Parameters:
        -----------
            context: The browsing context ID to activate.

        Raises:
        ------
            Exception: If the browsing context is not a top-level traversable.
        r%   zbrowsingContext.activateNr   r   r   )r+   r%   paramss      r   activatezBrowsingContext.activate  s(     W%		/*DfMNr   viewportoriginformatclipc                 |    ||d}|||d<   |||d<   | j                   j                  t        d|            }|d   S )a  Captures an image of the given navigable, and returns it as a Base64-encoded string.

        Parameters:
        -----------
            context: The browsing context ID to capture.
            origin: The origin of the screenshot, either "viewport" or "document".
            format: The format of the screenshot.
            clip: The clip rectangle of the screenshot.

        Returns:
        -------
            str: The Base64-encoded screenshot.
        )r%   r  r  r  z!browsingContext.captureScreenshotdatar  )r+   r%   r  r  r  r  results          r   capture_screenshotz"BrowsingContext.capture_screenshot  sT    ( .5!G%F8!F6N""?3VX^#_`f~r   prompt_unloadc                 X    ||d}| j                   j                  t        d|             y)a/  Closes a top-level traversable.

        Parameters:
        -----------
            context: The browsing context ID to close.
            prompt_unload: Whether to prompt to unload.

        Raises:
        ------
            Exception: If the browsing context is not a top-level traversable.
        )r%   promptUnloadzbrowsingContext.closeNr  )r+   r%   r   r  s       r   closezBrowsingContext.close  s(     %mD		/*A6JKr   rJ   reference_context
backgroundrA   c                     d|i}|||d<   |||d<   |||d<   | j                   j                  t        d|            }|d   S )a   Creates a new navigable, either in a new tab or in a new window, and returns its navigable id.

        Parameters:
        -----------
            type: The type of the new navigable, either "tab" or "window".
            reference_context: The reference browsing context ID.
            background: Whether to create the new navigable in the background.
            user_context: The user context ID.

        Returns:
        -------
            str: The browsing context ID of the created navigable.
        rJ   referenceContextr%  rF   zbrowsingContext.creater%   r  )r+   rJ   r$  r%  rA   r  r  s          r   createzBrowsingContext.create   se    ( #)$():F%&!#-F< #$0F=!""?3KV#TUi  r   	max_depthrootc                     i }|||d<   |||d<   | j                   j                  t        d|            }|d   D cg c]  }t        j	                  |       c}S c c}w )a  Returns a tree of all descendent navigables including the given parent itself, or all top-level contexts
        when no parent is provided.

        Parameters:
        -----------
            max_depth: The maximum depth of the tree.
            root: The root browsing context ID.

        Returns:
        -------
            List[BrowsingContextInfo]: A list of browsing context information.
        maxDepthr*  zbrowsingContext.getTreer   )r   r   r   r>   r:   )r+   r)  r*  r  r  r%   s         r   get_treezBrowsingContext.get_tree?  si    " "$ !*F:!F6N""?3Lf#UVFLZFXY7#--g6YYYs   Aacceptrh   c                 r    d|i}|||d<   |||d<   | j                   j                  t        d|             y)zAllows closing an open prompt.

        Parameters:
        -----------
            context: The browsing context ID.
            accept: Whether to accept the prompt.
            user_text: The text to enter in the prompt.
        r%   Nr.  rl   z browsingContext.handleUserPromptr  )r+   r%   r.  rh   r  s        r   handle_user_promptz"BrowsingContext.handle_user_promptY  sH     #,W!5%F8 !*F:		/*LfUVr   locatormax_node_countserialization_optionsstart_nodesc                     ||d}|||d<   |||d<   |||d<   | j                   j                  t        d|            }|d   S )a  Returns a list of all nodes matching the specified locator.

        Parameters:
        -----------
            context: The browsing context ID.
            locator: The locator to use.
            max_node_count: The maximum number of nodes to return.
            serialization_options: The serialization options.
            start_nodes: The start nodes.

        Returns:
        -------
            List[Dict]: A list of nodes.
        )r%   r1  maxNodeCountserializationOptions
startNodeszbrowsingContext.locateNodesnodesr  )r+   r%   r1  r2  r3  r4  r  r  s           r   locate_nodeszBrowsingContext.locate_nodeso  sf    , .5!I%%3F>" ,-BF)*"#.F< ""?3PRX#YZgr   r(   waitc                 h    ||d}|||d<   | j                   j                  t        d|            }|S )aF  Navigates a navigable to the given URL.

        Parameters:
        -----------
            context: The browsing context ID.
            url: The URL to navigate to.
            wait: The readiness state to wait for.

        Returns:
        -------
            Dict: A dictionary containing the navigation result.
        )r%   r(   r;  zbrowsingContext.navigater  )r+   r%   r(   r;  r  r  s         r   navigatezBrowsingContext.navigate  s>    $ %S1!F6N""?3Mv#VWr   marginorientationpagepage_rangesscaleshrink_to_fitc	                     |||||d}	|||	d<   |||	d<   |||	d<   | j                   j                  t        d|	            }
|
d   S )a  Creates a paginated representation of a document, and returns it as a PDF document represented as a
        Base64-encoded string.

        Parameters:
        -----------
            context: The browsing context ID.
            background: Whether to include the background.
            margin: The margin parameters.
            orientation: The orientation, either "portrait" or "landscape".
            page: The page parameters.
            page_ranges: The page ranges.
            scale: The scale.
            shrink_to_fit: Whether to shrink to fit.

        Returns:
        -------
            str: The Base64-encoded PDF document.
        )r%   r%  r?  rB  shrinkToFitr>  r@  
pageRangeszbrowsingContext.printr  r  )r+   r%   r%  r>  r?  r@  rA  rB  rC  r  r  s              r   printzBrowsingContext.print  sq    < $&(
 %F8!F6N"#.F< ""?3JF#STf~r   ignore_cachec                 t    d|i}|||d<   |||d<   | j                   j                  t        d|            }|S )aA  Reloads a navigable.

        Parameters:
        -----------
            context: The browsing context ID.
            ignore_cache: Whether to ignore the cache.
            wait: The readiness state to wait for.

        Returns:
        -------
            Dict: A dictionary containing the navigation result.
        r%   ignoreCacher;  zbrowsingContext.reloadr  )r+   r%   rH  r;  r  r  s         r   reloadzBrowsingContext.reload  sN    $ #,W!5#$0F=!!F6N""?3KV#TUr   device_pixel_ratiouser_contextsc                     i }|||d<   |||d<   |||d<   |||d<   | j                   j                  t        d|             y)a  Modifies specific viewport characteristics on the given top-level traversable.

        Parameters:
        -----------
            context: The browsing context ID.
            viewport: The viewport parameters.
            device_pixel_ratio: The device pixel ratio.
            user_contexts: The user context IDs.

        Raises:
        ------
            Exception: If the browsing context is not a top-level traversable.
        Nr%   r  devicePixelRatiouserContextszbrowsingContext.setViewportr  )r+   r%   r  rL  rM  r  s         r   set_viewportzBrowsingContext.set_viewport  sd    ( "$ 'F9!)F:));F%&$%2F>"		/*GPQr   deltac                 Z    ||d}| j                   j                  t        d|            }|S )a,  Traverses the history of a given navigable by a delta.

        Parameters:
        -----------
            context: The browsing context ID.
            delta: The delta to traverse by.

        Returns:
        -------
            Dict: A dictionary containing the traverse history result.
        )r%   rR  zbrowsingContext.traverseHistoryr  )r+   r%   rR  r  r  s        r   traverse_historyz BrowsingContext.traverse_history  s0     %u5""?3TV\#]^r   r   r   r   c                 <    | j                   j                  |||      S )aL  Add an event handler to the browsing context.

        Parameters:
        ----------
            event: The event to subscribe to.
            callback: The callback function to execute on event.
            contexts: The browsing context IDs to subscribe to.

        Returns:
        -------
            int: callback id
        )r  r   )r+   r   r   r   s       r   r   z!BrowsingContext.add_event_handler"  s     ""44UHhOOr   r   c                 <    | j                   j                  ||       y)zRemove an event handler from the browsing context.

        Parameters:
        ----------
            event: The event to unsubscribe from.
            callback_id: The callback id to remove.
        N)r  r   )r+   r   r   s      r   r   z$BrowsingContext.remove_event_handler1  s     	00Dr   c                 8    | j                   j                          yr   )r  r   )r+   s    r   r   z$BrowsingContext.clear_event_handlers;  s    002r   )r  NN)F)NFNrN   )NNNr*   )FNportraitNNg      ?T)NNNNr   )0r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r,   r;   rI   r6   r  r  r   r<   r  rm   r#  r(  r8   r>   r-  r0  r:  r=  r   floatrG  rK  rQ  rT  r   r   r   r   r   r   r   r   r     sw   < ''8:Z\jk()<>`brs)*>@bdtu#N4QS^_*!#FHY 
 **>@cevw&'8:Z\jkF$:DA)*>@cevw +"$IK^!
 ))<>`brs)*>@cevw)*>@bdtu)*>@bdtu%M*F .S	 . .O O O" !!%#  	
 tn 
:LS L L$ L$ ,0 &*!! $C=! 	!
 sm! 
!B $("ZC=Z smZ 
!	"	Z: "&#'	WW W C=	W
 
W4 )-04,0  !	
  (~ d4j) 
dJ #	  sm	
 
8 !!%%#7;",, , 	,
 , tn, d5c?34, , , 
,b (,"	 tn sm	
 
: "&#'.2-1R#R 4.R %UO	R
  S	*R 
R@ C D  Ps Ph P(SWX[S\J] Pil PE# EC ED E3r   r   )(r   dataclassesr   typingr   r   r   r   %selenium.webdriver.common.bidi.commonr   &selenium.webdriver.common.bidi.sessionr	   r   r   r$   r>   rP   r[   rf   ro   rt   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r^     sU  $  ! 1 1 A :  +8 +8\M
 M
`
n 
>7
 7
t0
 0
f)
 )
X
^ 
:!
^ !
HO O(	3 	3	3 	3	. 	.	. 	.	. 	.	. 	.	. 	.	. 	.	. 	.7 76 66 64 41 1   b' b'J}3 }3r   