
    ,h]                        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	      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      Z G d d      Zy)    )Dict)List)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     q/var/www/html/Resume-Scraper/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__0   s      $"r   jsonreturnc                      | |j                  d      |j                  d      |j                  d      |j                  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%   r&   r'   r(   r+   getclsr.   s     r   	from_jsonzNavigationInfo.from_json<   s@     HHY'xx-hh{+	
 	
r   N)r   r   r   r   strr   intr-   classmethodr   r5   r   r   r   r$   r$   -   sW    4

 SM
 	

 
 
T 
&6 
 
r   r$   c                   |    e Zd ZdZ	 	 	 	 ddededeed       dee   de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parentuser_contextoriginal_openerclient_windowc                 f    || _         || _        || _        || _        || _        || _        || _        y r*   r%   r(   r;   r<   r=   r>   r?   )r,   r%   r(   r;   r<   r=   r>   r?   s           r   r-   zBrowsingContextInfo.__init__S   s8      (.*r   r.   r/   c                 f   d}|j                  d      1|j                  d      D cg c]  }t        j                  |       }} | |j                  d      |j                  d      ||j                  d      |j                  d      |j                  d      |j                  d      	      S c c}w )
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;   r%   r(   r<   userContextoriginalOpenerclientWindowrA   )r2   r:   r5   )r4   r.   r;   childs       r   r5   zBrowsingContextInfo.from_jsone   s     88J+JN((S]J^_+55e<_H_HHY'88H%-0 HH%56((>2
 	
 `s   B.NNNN)r   r   r   r   r6   r   r   r-   r8   r   r5   r   r   r   r:   r:   P   s    3 !%&*)-'+++ + 4 567	+
 + sm+ "#+  }+$ 
T 
&; 
 
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-   rJ   )r,   r%   r&   r'   r(   rJ   	__class__s         r   r-   z DownloadWillBeginParams.__init__   s      	*i="4r   r.   r/   c           	           | |j                  d      |j                  d      |j                  d      |j                  d      |j                  d            S )a$  Creates a DownloadWillBeginParams instance from a dictionary.

        Parameters:
        -----------
            json: A dictionary containing the download parameters.

        Returns:
        -------
            DownloadWillBeginParams: A new instance of DownloadWillBeginParams.
        r%   r&   r'   r(   suggestedFilename)r%   r&   r'   r(   rJ   r1   r3   s     r   r5   z!DownloadWillBeginParams.from_json   sM     HHY'xx-hh{+#xx(;<
 	
r   )r   r   r   r   r6   r   r7   r-   r8   r   r5   __classcell__)rM   s   @r   rI   rI      sb    5	5	5 SM	5 		5
 	5  	5 
T 
&? 
 
r   rI   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messagetypedefault_valuec                 J    || _         || _        || _        || _        || _        y r*   r%   rS   rT   rU   rV   )r,   r%   rS   rT   rU   rV   s         r   r-   zUserPromptOpenedParams.__init__   s(     	*r   r.   r/   c           	           | |j                  d      |j                  d      |j                  d      |j                  d      |j                  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%   rS   rT   rU   defaultValuerX   r1   r3   s     r   r5   z UserPromptOpenedParams.from_json   sM     HHY'HHY'HHY'&!((>2
 	
r   r*   )
r   r   r   r   r6   r   r-   r8   r   r5   r   r   r   rR   rR      sf    4 (,++ + 	+
 +  }+ 
T 
&> 
 
r   rR   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%   acceptedrU   	user_textc                 <    || _         || _        || _        || _        y r*   r%   r]   rU   r^   )r,   r%   r]   rU   r^   s        r   r-   zUserPromptClosedParams.__init__   s       	"r   r.   r/   c                      | |j                  d      |j                  d      |j                  d      |j                  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%   r]   rU   userTextr`   r1   r3   s     r   r5   z UserPromptClosedParams.from_json   sA     HHY'XXj)&!hhz*	
 	
r   r*   )r   r   r   r   r6   boolr   r-   r8   r   r5   r   r   r   r\   r\      s\    4 $(
#
# 
# 	
#
 C=
# 
T 
&> 
 
r   r\   c                   :    e Zd ZdZdedefdZededd fd       Zy)	HistoryUpdatedParamsz(Parameters for the historyUpdated event.r%   r(   c                      || _         || _        y r*   r%   r(   )r,   r%   r(   s      r   r-   zHistoryUpdatedParams.__init__   s    
 r   r.   r/   c                 R     | |j                  d      |j                  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%   r(   rg   r1   r3   s     r   r5   zHistoryUpdatedParams.from_json   s(     HHY'
 	
r   N	r   r   r   r   r6   r-   r8   r   r5   r   r   r   re   re      s?    2  
T 
&< 
 
r   re   c                   6    e Zd ZdZdefdZededd fd       Zy)BrowsingContextEventz'Base class for browsing context events.event_classc                      || _         || _        y r*   )rl   params)r,   rl   kwargss      r   r-   zBrowsingContextEvent.__init__  s    &r   r.   r/   c                 4     | dd|j                  d      i|S )a  Creates a BrowsingContextEvent instance from a dictionary.

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

        Returns:
        -------
            BrowsingContextEvent: A new instance of BrowsingContextEvent.
        rl   r   r1   r3   s     r   r5   zBrowsingContextEvent.from_json  s      ?txx6?$??r   Nri   r   r   r   rk   rk   	  s8    1C  @T @&< @ @r   rk   c                      e Zd ZdZdddddddd	d
dddddZd ZdeddfdZ	 	 	 dFdededee	   dee	   def
dZ
dGdededdfdZ	 	 	 dHdedee   dedee   def
d Z	 	 dId!ee   d"ee   dee   fd#Z	 	 dIded$ee   d%ee   ddfd&Z	 	 	 dJded'e	d(ee   d)ee	   d*eee	      dee	   fd+Z	 dKded,ed-ee   de	fd.Z	 	 	 	 	 	 	 dLdeded/ee	   d0ed1ee	   d2eeeeef         d3ed4edefd5Z	 	 dIded6ee   d-ee   de	fd7Z	 	 	 	 dMdee   dee	   d8ee   d9eee      ddf
d:Zded;ede	fd<Zd=ed>edefd?ZdKd@ed>edAeee      defdBZd@edCeddfdDZdNdEZy)OBrowsingContextz2BiDi implementation of the browsingContext module.zbrowsingContext.contextCreatedz browsingContext.contextDestroyedz browsingContext.domContentLoadedz!browsingContext.downloadWillBeginz!browsingContext.fragmentNavigatedzbrowsingContext.historyUpdatedzbrowsingContext.loadz!browsingContext.navigationAbortedz#browsingContext.navigationCommittedz browsingContext.navigationFailedz!browsingContext.navigationStartedz browsingContext.userPromptClosedz browsingContext.userPromptOpened)context_createdcontext_destroyeddom_content_loadeddownload_will_beginfragment_navigatedhistory_updatedloadnavigation_abortednavigation_committednavigation_failednavigation_starteduser_prompt_closeduser_prompt_openedc                 .    || _         i | _        i | _        y r*   )connsubscriptions	callbacks)r,   r   s     r   r-   zBrowsingContext.__init__2  s    	r   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   executer   )r,   r%   rn   s      r   activatezBrowsingContext.activate7  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   rn   results          r   capture_screenshotz"BrowsingContext.capture_screenshotE  sT    ( %7%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   rn   s       r   closezBrowsingContext.closeb  s(     %mD		/*A6JKr   rU   reference_context
backgroundr=   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.
        rU   referenceContextr   rC   zbrowsingContext.creater%   r   )r,   rU   r   r   r=   rn   r   s          r   createzBrowsingContext.createq  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.getTreecontexts)r   r   r   r:   r5   )r,   r   r   rn   r   r%   s         r   get_treezBrowsingContext.get_tree  si       !*F:!F6N""?3Lf#UVFLZFXY7#--g6YYYs   Aacceptr^   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   rb   z browsingContext.handleUserPromptr   )r,   r%   r   r^   rn   s        r   handle_user_promptz"BrowsingContext.handle_user_prompt  sH     W%%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%   r   maxNodeCountserializationOptions
startNodeszbrowsingContext.locateNodesnodesr   )r,   r%   r   r   r   r   rn   r   s           r   locate_nodeszBrowsingContext.locate_nodes  sf    , %9%%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.
        rg   r   zbrowsingContext.navigater   )r,   r%   r(   r   rn   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   r   shrinkToFitr   r   
pageRangeszbrowsingContext.printr   r   )r,   r%   r   r   r   r   r   r   r   rn   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%   r   r   rn   r   s         r   reloadzBrowsingContext.reload&  sN    $ W%#$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   r   r   rn   s         r   set_viewportzBrowsingContext.set_viewportA  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%   r   zbrowsingContext.traverseHistoryr   )r,   r%   r   rn   r   s        r   traverse_historyz BrowsingContext.traverse_historya  s0     %u5""?3TV\#]^r   
event_namecallbackc                      t              } fd} j                  j                  ||      } j                  v r  j                     j	                  |       |S |g j                  <   |S )a%  Set a callback function to subscribe to a browsing context event.

        Parameters:
        ----------
            event_name: The event to subscribe to.
            callback: The callback function to execute on event.

        Returns:
        -------
            int: callback id
        c                    j                   d   k(  sj                   d   k(  r(t        j                  | j                        } |       y j                   d   k(  r(t        j                  | j                        } |       y j                   d   k(  r(t
        j                  | j                        } |       y j                   d   k(  r(t        j                  | j                        } |       y j                   d   k(  r(t        j                  | j                        } |       y t        j                  | j                        } |       y )Nrs   rt   rv   r   r~   rx   )	EVENTSr:   r5   rn   rI   rR   r\   re   r$   )
event_datainforn   r   r   r,   s      r   	_callbackz,BrowsingContext._on_event.<locals>._callback  s"   T[[):;;zT[[YlMm?m*44Z5F5FGt{{+@AA0:::;L;LM t{{+?@@/99*:K:KL t{{+?@@/99*:K:KL t{{+<==-77
8I8IJ  &//
0A0ABr   )rk   r   add_callbackr   append)r,   r   r   eventr   callback_ids   ```   r   	_on_eventzBrowsingContext._on_eventq  so     %Z0	* ii,,UI>'NN:&--k:  +6DNN:&r   r   r   c                    	 | j                   |   }| j                  ||      }|| j                  v r | j                  |   j                  |       |S d|gi}|||d<   t        | j                        }| j                  j                   |j                  di |       |g| j                  |<   |S # t        $ r t        d| d      w xY w)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
        Event 
 not foundeventsbrowsingContextsr   )
r   KeyError	Exceptionr   r   r   r	   r   r   	subscribe)r,   r   r   r   r   r   rn   sessions           r   add_event_handlerz!BrowsingContext.add_event_handler  s    	8U+J nnZ:+++z*11+>  -F#-5)*dii(GII/g//9&9:.9]Dz*  	8fUG:677	8s   B/ /Cr   c                    	 | j                   |   }t        |      }| j                  j                  ||       | j                  |   j                  |       t        | j                  |         dk(  rSd|gi}t        | j                        }| j                  j                   |j                  di |       | j                  |= yy# t        $ r t        d| d      w xY w)zRemove an event handler from the browsing context.

        Parameters:
        ----------
            event: The event to unsubscribe from.
            callback_id: The callback id to remove.
        r   r   r   r   Nr   )r   r   r   rk   r   remove_callbackr   removelenr	   r   unsubscribe)r,   r   r   r   rn   r   s         r   remove_event_handlerz$BrowsingContext.remove_event_handler  s    	8U+J %Z0		!!%5:&--k:t!!*-.!3-Fdii(GII1g11;F;<"":.	 4  	8fUG:677	8s   C Cc                 4   | j                   D ]  }t        |      }| j                   |   D ]  }| j                  j                  ||         d|gi}t	        | j                        }| j                  j                   |j                  di |        i | _         y)z3Clear all event handlers from the browsing context.r   Nr   )r   rk   r   r   r	   r   r   )r,   r   r   r   rn   r   s         r   clear_event_handlersz$BrowsingContext.clear_event_handlers  s    ,, 	=J(4E#11*= >		))%=>-Fdii(GII1g11;F;<	=  r   )r   NN)F)NFN)NN)NNNr*   )FNportraitNNg      ?TrG   )r/   N) r   r   r   r   r   r-   r6   r   r   r   r   rc   r   r   r7   r   r:   r   r   r   r   r   floatr   r   r   r   callabler   r   r   r   r   r   r   rr   rr     s   < <?@BA;&A E?A@@F 
O O O" !!%#  	
 tn 
:LS L L$ L$ ,0 &*!! $C=! 	!
 sm! 
!B $("ZC=Z smZ 
!	"	Z8 "&#'	WW W C=	W
 
W4 )-04,0  !	
  (~ d4j) 
dJ #	  sm	
 
8 !!%%#7;"++ + 	+
 + tn+ d5c?34+ + + 
+` (,"	 tn sm	
 
: "&#'.2-1R#R 4.R %UO	R
  S	*R 
R@ C D  *C *8 * *Xs h (SWX[S\J] il @/# /C /D /.	 r   rr   N)typingr   r   r   r   %selenium.webdriver.common.bidi.commonr   r   r	   r   r   r$   r:   rI   rR   r\   re   rk   rr   r   r   r   <module>r      s   $     A    
  
F-
 -
` 
n  
F#
 #
L 
  
F
 
:@ @,~  ~ r   