
    i                        d dl 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  ed      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/ d0      Z! G d1 d2      Z" G d3 d4      Z# G d5 d6      Z$ G d7 d8      Z% G d9 d:      Z&e G d; d<             Z' G d= d>      Z( G d? d@      Z)y)A    N)Callable)	dataclass)Any)Sentinel)command_builder)Session	UNDEFINEDc                       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     k/var/www/html/BatchJob/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dz  dedefdZeded	d fd
       Z	y)NavigationInfoz*Provides details of an ongoing navigation.context
navigationN	timestampurlc                 <    || _         || _        || _        || _        y N)r%   r&   r'   r(   )selfr%   r&   r'   r(   s        r   __init__zNavigationInfo.__init__3   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 )
zCreates a NavigationInfo instance from a dictionary.

        Args:
            json: A dictionary containing the navigation information.

        Returns:
            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   
r   r   r   r   r6   r8   r,   classmethoddictr:   r   r   r   r$   r$   0   sW    4

 $J
 	

 
 8T 8&6 8 8r   r$   c                   l    e Zd ZdZ	 	 ddededed    dz  dedededz  d	edz  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@   rC   rB   rD   rA   )r+   r%   r(   r@   rA   rB   rC   rD   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 )zCreates a BrowsingContextInfo instance from a dictionary.

        Args:
            json: A dictionary containing the browsing context information.

        Returns:
            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   rC   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@   rA   rB   rC   rD   )
r4   r5   listr7   r=   typeappendr?   r:   r6   )r9   r-   r@   raw_childrenchildr%   r(   rC   rB   rD   rA   s              r   r:   zBrowsingContextInfo.from_jsonq   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   rJ   r,   r<   r=   r:   r   r   r   r?   r?   \   s    3 "&*++ + ,-4	+
 + + d
+ t+$ 5
T 5
&; 5
 5
r   r?   c            
       X     e Zd ZdZdededz  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&   Nr'   r(   suggested_filenamec                 8    t         |   ||||       || _        y r*   )superr,   rR   )r+   r%   r&   r'   r(   rR   	__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(   rR   )
r$   r:   r4   r5   r6   r7   r%   r&   r'   r(   )r9   r-   nav_inforR   s       r   r:   z!DownloadWillBeginParams.from_json   sp    !++D1!XX&9:%Z8JC-PQRR$$**((1
 	
r   )r   r   r   r   r6   r8   r,   r<   r=   r:   __classcell__rU   s   @r   rQ   rQ      sb    5	5	5 $J	5 		5
 	5  	5 
T 
&? 
 
r   rQ   c                   P    e Zd ZdZ	 ddedededededz  f
dZed	ed
d fd       Zy)UserPromptOpenedParamsz*Parameters for the userPromptOpened event.Nr%   handlermessagerK   default_valuec                 J    || _         || _        || _        || _        || _        y r*   r%   r]   r^   rK   r_   )r+   r%   r]   r^   rK   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 )zCreates a UserPromptOpenedParams instance from a dictionary.

        Args:
            json: A dictionary containing the user prompt parameters.

        Returns:
            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 stringrK   %type is required and must be a stringdefaultValuez)defaultValue must be a string if providedra   )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\      sf    4 %)++ + 	+
 + Tz+ #
T #
&> #
 #
r   r\   c            
       L    e Zd ZdZ	 ddededededz  fdZeded	d fd
       Z	y)UserPromptClosedParamsz*Parameters for the userPromptClosed event.Nr%   acceptedrK   	user_textc                 <    || _         || _        || _        || _        y r*   r%   rh   rK   ri   )r+   r%   rh   rK   ri   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 )
zCreates a UserPromptClosedParams instance from a dictionary.

        Args:
            json: A dictionary containing the user prompt closed parameters.

        Returns:
            A new instance of UserPromptClosedParams.
        r%   r0   rh   z*accepted is required and must be a booleanrK   rc   userTextz%userText must be a string if providedrk   )r4   r5   r6   r7   bool)r9   r-   r%   rh   re   ri   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   rn   r,   r<   r=   r:   r   r   r   rg   rg     s\    4 !%
#
# 
# 	
#
 :
# 
T 
&> 
 
r   rg   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__5  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 )	zCreates a HistoryUpdatedParams instance from a dictionary.

        Args:
            json: A dictionary containing the history updated parameters.

        Returns:
            A new instance of HistoryUpdatedParams.
        r%   r0   r'   r   r1   r(   r2   rr   r3   )r9   r-   r%   r'   r(   s        r   r:   zHistoryUpdatedParams.from_json?  s     ((9%?*Wc":GHHHH[)	Jy#$>)a-WXXhhuo;jc2CDD
 	
r   Nr;   r   r   r   rp   rp   2  sI    2  	 
T 
&< 
 
r   rp   c                   X     e Zd Z	 ddededz  dededef
 fdZeded	d fd
       Z xZ	S )DownloadCanceledParamsr%   r&   Nr'   r(   statusc                 8    t         |   ||||       || _        y r*   )rT   r,   rv   )r+   r%   r&   r'   r(   rv   rU   s         r   r,   zDownloadCanceledParams.__init__]  s     	*i=r   r-   r.   c                     t         j                  |      }|j                  d      }||dk7  rt        d       | |j                  |j
                  |j                  |j                  |      S )Nrv   canceledz)status is required and must be 'canceled')r%   r&   r'   r(   rv   )r$   r:   r4   r7   r%   r&   r'   r(   )r9   r-   rX   rv   s       r   r:   z DownloadCanceledParams.from_jsonh  sj    !++D1(#>Vz1HII$$**((
 	
r   )ry   
r   r   r   r6   r8   r,   r<   r=   r:   rY   rZ   s   @r   ru   ru   \  sd     !		 $J	 		
 	 	 
T 
&> 
 
r   ru   c                   d     e Zd Z	 	 ddededz  dededededz  f fdZed	ed
d fd       Z xZ	S )DownloadCompleteParamsNr%   r&   r'   r(   rv   filepathc                 F    t         |   ||||       || _        || _        y r*   )rT   r,   rv   r}   )r+   r%   r&   r'   r(   rv   r}   rU   s          r   r,   zDownloadCompleteParams.__init__z  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 )Nrv   r   z)status is required and must be 'complete'r}   z%filepath must be a string if provided)r%   r&   r'   r(   rv   r}   )
r$   r:   r4   r7   r5   r6   r%   r&   r'   r(   )r9   r-   rX   rv   r}   s        r   r:   z DownloadCompleteParams.from_json  s    !++D1(#>Vz1HII88J'
8S(ADEE$$**((
 	
r   )r   Nrz   rZ   s   @r   r|   r|   y  su     !#!! $J! 	!
 ! ! *! 
T 
&> 
 
r   r|   c                   <    e Zd ZdZdeez  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      )Nrv   ry   r   z.status must be either 'canceled' or 'complete')r4   ru   r:   r|   r7   )r9   r-   rv   s      r   r:   zDownloadEndParams.from_json  sZ    (#Z-77=>>z!-77=>>MNNr   N)
r   r   r   r   ru   r|   r,   r<   r=   r:   r   r   r   r   r     s?    ///2HH/ 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*   )rQ   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_json,      %//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*   )rg   r:   r   s     r   r:   zUserPromptClosed.from_json6  r   r   Nr   r   r   r   r   r   1  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*   )rp   r:   r   s     r   r:   zHistoryUpdated.from_json@  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_jsonJ  s     **400r   Nr   r   r   r   r   r   E  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__rK   r   r   r   r   r   O  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   dz  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   dz  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__Y  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_eventb  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.

        Args:
            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_eventh  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)z~Unsubscribe from a BiDi event if no more callbacks exist.

        Args:
            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_eventu  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   rL   )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   rJ   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   rJ   r   r   r8   r   r   r   r   r   r   r   r   r   r   r   V  s    P3Dk1A,B 3C K 4S 4DI<L 4X\ 43 3 3?3 ?S ?T ?2 2# 2RV 2s h $s)VZJZ fi 
=# 
=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d#z  d(ed#z  d ef
d)ZdUd"ed*ed d#fd+Z	 	 	 dVd,ed-ed#z  d.ed/ed#z  d ef
d0Z	 	 dWd1ed#z  d2ed#z  d ee    fd3Z!	 	 dWd"ed4ed#z  d5ed#z  d d#fd6Z"	 	 	 dXd"ed7ed8ed#z  d9ed#z  d:ee   d#z  d ee   fd;Z#	 dYd"ed<ed=ed#z  d efd>Z$	 	 	 	 	 	 	 dZd"ed.ed?ed#z  d@edAed#z  dBeeez     d#z  dCe%dDed efdEZ&	 	 dWd"edFed#z  d=ed#z  d efdGZ'd#e(e(d#fd"ed#z  d%ed#z  e)z  dHe%d#z  e)z  dIee   d#z  d d#f
dJZ*d"edKed efdLZ+dYdMedNe,dOee   d#z  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:
            A list of event names that can be used with event handlers.
        )rJ   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)zActivates and focuses the given top-level traversable.

        Args:
            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.

        Args:
            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:
            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.

        Args:
            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   rK   reference_context
backgroundrB   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.

        Args:
            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:
            The browsing context ID of the created navigable.
        rK   referenceContextr&  rG   zbrowsingContext.creater%   r  )r+   rK   r%  r&  rB   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  Get a tree of all descendent navigables including the given parent itself.

        Returns a tree of all descendent navigables including the given parent itself, or all top-level contexts
        when no parent is provided.

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

        Returns:
            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acceptri   c                 r    d|i}|||d<   |||d<   | j                   j                  t        d|             y)zAllows closing an open prompt.

        Args:
            context: The browsing context ID.
            accept: Whether to accept the prompt.
            user_text: The text to enter in the prompt.
        r%   Nr/  rm   z browsingContext.handleUserPromptr  )r+   r%   r/  ri   r  s        r   handle_user_promptz"BrowsingContext.handle_user_promptI  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.

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

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

        Returns:
            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,  Create a paginated PDF representation of the document as a Base64-encoded string.

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

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

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

        Args:
            context: The browsing context ID.
            viewport: The viewport parameters - {"width": <int>, "height": <int>} (`None` resets to default).
            device_pixel_ratio: The device pixel ratio (`None` resets to default).
            user_contexts: The user context IDs.

        Raises:
            Exception: If the browsing context is not a top-level traversable
            ValueError: If neither `context` nor `user_contexts` is provided
            ValueError: If both `context` and `user_contexts` are provided
        Nz-Cannot specify both context and user_contextsz,Must specify either context or user_contextsr%   userContextsr  devicePixelRatiozbrowsingContext.setViewport)r7   r	   r   r   r   )r+   r%   r  rM  rN  r  s         r   set_viewportzBrowsingContext.set_viewport  s    ( =#<LMM?}4KLL!# 'F9&%2F>"9$!)F:Y.);F%&		/*GPQr   deltac                 Z    ||d}| j                   j                  t        d|            }|S )zTraverses the history of a given navigable by a delta.

        Args:
            context: The browsing context ID.
            delta: The delta to traverse by.

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

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

        Returns:
            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.

        Args:
            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_handler  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)NFNrO   )NNNr*   )FNportraitNNg      ?Tr   )0r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r,   r<   rJ   r6   r  r  r=   r   rn   r$  r)  r8   r?   r.  r1  r;  r>  floatrH  rL  r	   r   rR  rU  r   r   r   r   r   r   r   r  r    sy   < ''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 !"   t	
 Tk 
6LS L L$ L  )- #'!! :! 	!
 Dj! 
!> !%Z:Z DjZ 
!	"	Z: # $	WW tW :	W
 
W2 &*-1)-  d
	
  $d{ $Z$& 
dF  	  Dj	
 
4 !"% .2")) ) t	)
 ) Tk) #)_t+) ) ) 
)\ %)	 Tk Dj	
 
6 #+46?*.$Rt$R +($R "DL83	$R
 Cy4'$R 
$RL C D Ps Ph P$s)VZJZ Pfi PE# EC ED E3r   r  )*r   collections.abcr   dataclassesr   typingr   typing_extensionsr   %selenium.webdriver.common.bidi.commonr   &selenium.webdriver.common.bidi.sessionr   r	   r   r   r$   r?   rQ   r\   rg   rp   ru   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   <module>ra     sb  $  $ !  & A :[!	  )8 )8XK
 K
\
n 
>5
 5
p.
 .
b'
 '
T
^ 
:!
^ !
HO O(	3 	3	3 	3	. 	.	. 	.	. 	.	. 	.	. 	.	. 	.	. 	.7 76 66 64 41 1   `' `'Fk3 k3r   