o
    M,h                     @   s  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 dZG dd	 d	ZG d
d dZG dd dZG dd deZG dd dZG dd dZG dd dZG dd deZG dd deZG dd dZG dd dZG dd dZG d d! d!ZG d"d# d#ZG d$d% d%ZG d&d' d'ZG d(d) d)ZG d*d+ d+ZG d,d- d-ZG d.d/ d/Z G d0d1 d1Z!G d2d3 d3Z"G d4d5 d5Z#G d6d7 d7Z$eG d8d9 d9Z%G d:d; d;Z&G d<d= d=Z'dS )>    N)	dataclass)AnyCallableOptionalUnion)command_builder)Sessionc                   @   s   e Zd ZdZdZdZdZdS )ReadinessStatezSRepresents the stage of document loading at which a navigation command will return.noneinteractivecompleteN)__name__
__module____qualname____doc__NONEINTERACTIVECOMPLETE r   r   {/var/www/www-root/data/www/bot.pdev.uz/venv/lib/python3.10/site-packages/selenium/webdriver/common/bidi/browsing_context.pyr	      s
    r	   c                   @   s    e Zd ZdZdZdZdZdZdS )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    r   c                   @   sD   e Zd ZdZdedee dedefddZede	d	d fd
dZ
dS )NavigationInfoz*Provides details of an ongoing navigation.context
navigation	timestampurlc                 C      || _ || _|| _|| _d S N)r    r!   r"   r#   )selfr    r!   r"   r#   r   r   r   __init__.      
zNavigationInfo.__init__jsonreturnc                 C   s   | d}|du st|tstd| d}|dur$t|ts$td| d}|du s6t|tr6|dk r:td| d	}|du sHt|tsLt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    N(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#   r   r   r   	from_json:   s   



zNavigationInfo.from_jsonN)r   r   r   r   r1   r   r3   r'   classmethoddictr5   r   r   r   r   r   +   s    
r   c                   @   sb   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
dZede	dd fddZ
dS )BrowsingContextInfoz)Represents the properties of a navigable.Nr    r#   childrenclient_windowuser_contextparentoriginal_openerc                 C   s.   || _ || _|| _|| _|| _|| _|| _d S r%   )r    r#   r9   r<   r;   r=   r:   )r&   r    r#   r9   r:   r;   r<   r=   r   r   r   r'   \   s   

zBrowsingContextInfo.__init__r)   r*   c              	   C   sP  d}| d}|dur1t|tstdg }|D ]}t|ts(tdt| |t| q| d}|du s?t|t	sCtd| d}|du sQt|t	sUtd| d	}|durgt|t	sgtd
| d}|du sut|t	sytd| d}	|	durt|	t	std| d}
|
du st|
t	std| ||||
|||	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.
        Nr9   z#children must be a list if providedz%Each child must be a dictionary, got r    r+   r#   r-   r<   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#   r9   r:   r;   r<   r=   )
r/   r0   listr2   r7   typeappendr8   r5   r1   )r4   r)   r9   raw_childrenchildr    r#   r<   r;   r=   r:   r   r   r   r5   n   sJ   








zBrowsingContextInfo.from_jsonNN)r   r   r   r   r1   r   rA   r'   r6   r7   r5   r   r   r   r   r8   Y   s*    	

r8   c                
       sP   e Zd ZdZdedee dededef
 fddZed	e	d
d fddZ
  ZS )DownloadWillBeginParamsz+Parameters for the downloadWillBegin event.r    r!   r"   r#   suggested_filenamec                       t  |||| || _d S r%   )superr'   rH   )r&   r    r!   r"   r#   rH   	__class__r   r   r'         
z DownloadWillBeginParams.__init__r)   r*   c                 C   sH   t |}|d}|d u st|tstd| |j|j|j|j	|dS )NsuggestedFilenamez2suggestedFilename is required and must be a string)r    r!   r"   r#   rH   )
r   r5   r/   r0   r1   r2   r    r!   r"   r#   )r4   r)   nav_inforH   r   r   r   r5      s   

z!DownloadWillBeginParams.from_json)r   r   r   r   r1   r   r3   r'   r6   r7   r5   __classcell__r   r   rK   r   rG      s    rG   c                   @   sL   e Zd ZdZ	ddededededee f
dd	Zed
edd fddZ	dS )UserPromptOpenedParamsz*Parameters for the userPromptOpened event.Nr    handlermessagerB   default_valuec                 C   s"   || _ || _|| _|| _|| _d S r%   r    rR   rS   rB   rT   )r&   r    rR   rS   rB   rT   r   r   r   r'      s
   
zUserPromptOpenedParams.__init__r)   r*   c                 C   s   | d}|du st|tstd| d}|du s t|ts$td| d}|du s2t|ts6td| d}|du sDt|tsHtd	| d
}|durZt|tsZtd| |||||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    Nr+   rR   z(handler is required and must be a stringrS   z(message is required and must be a stringrB   %type is required and must be a stringdefaultValuez)defaultValue must be a string if providedrU   )r/   r0   r1   r2   )r4   r)   r    rR   rS   
type_valuerT   r   r   r   r5      s,   




z UserPromptOpenedParams.from_jsonr%   )
r   r   r   r   r1   r   r'   r6   r7   r5   r   r   r   r   rQ      s     
rQ   c                
   @   sH   e Zd ZdZ	ddedededee fddZed	e	d
d fddZ
dS )UserPromptClosedParamsz*Parameters for the userPromptClosed event.Nr    acceptedrB   	user_textc                 C   r$   r%   r    rZ   rB   r[   )r&   r    rZ   rB   r[   r   r   r   r'     r(   zUserPromptClosedParams.__init__r)   r*   c                 C   s   | d}|du st|tstd| d}|du s t|ts$td| d}|du s2t|ts6td| d}|durHt|tsHtd	| ||||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    Nr+   rZ   z*accepted is required and must be a booleanrB   rV   userTextz%userText must be a string if providedr\   )r/   r0   r1   r2   bool)r4   r)   r    rZ   rX   r[   r   r   r   r5     s$   



z UserPromptClosedParams.from_jsonr%   )r   r   r   r   r1   r^   r   r'   r6   r7   r5   r   r   r   r   rY     s    
rY   c                   @   s<   e Zd ZdZdededefddZededd fd	d
Z	dS )HistoryUpdatedParamsz(Parameters for the historyUpdated event.r    r"   r#   c                 C   s   || _ || _|| _d S r%   r    r"   r#   )r&   r    r"   r#   r   r   r   r'   8  s   
zHistoryUpdatedParams.__init__r)   r*   c                 C   s   | d}|du st|tstd| d}|du s$t|tr$|dk r(td| d}|du s6t|ts:td| |||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    Nr+   r"   r   r,   r#   r-   r`   r.   )r4   r)   r    r"   r#   r   r   r   r5   B  s   


zHistoryUpdatedParams.from_jsonN)
r   r   r   r   r1   r3   r'   r6   r7   r5   r   r   r   r   r_   5  s    

r_   c                       sP   e Zd Z	ddedee dededef
 fddZed	ed
d fddZ	  Z
S )DownloadCanceledParamscanceledr    r!   r"   r#   statusc                    rI   r%   )rJ   r'   rc   )r&   r    r!   r"   r#   rc   rK   r   r   r'   b  rM   zDownloadCanceledParams.__init__r)   r*   c                 C   sF   t |}|d}|d u s|dkrtd| |j|j|j|j|dS )Nrc   rb   z)status is required and must be 'canceled')r    r!   r"   r#   rc   )r   r5   r/   r2   r    r!   r"   r#   )r4   r)   rO   rc   r   r   r   r5   m  s   

z DownloadCanceledParams.from_json)rb   r   r   r   r1   r   r3   r'   r6   r7   r5   rP   r   r   rK   r   ra   a  s    ra   c                       sZ   e Zd Z		ddedee dedededee f fd	d
Zededd fddZ	  Z
S )DownloadCompleteParamsr   Nr    r!   r"   r#   rc   filepathc                    s"   t  |||| || _|| _d S r%   )rJ   r'   rc   rf   )r&   r    r!   r"   r#   rc   rf   rK   r   r   r'     s   	
zDownloadCompleteParams.__init__r)   r*   c                 C   sl   t |}|d}|d u s|dkrtd|d}|d ur(t|ts(td| |j|j|j|j	||dS )Nrc   r   z)status is required and must be 'complete'rf   z%filepath must be a string if provided)r    r!   r"   r#   rc   rf   )
r   r5   r/   r2   r0   r1   r    r!   r"   r#   )r4   r)   rO   rc   rf   r   r   r   r5     s   


z DownloadCompleteParams.from_json)r   Nrd   r   r   rK   r   re   ~  s$    re   c                   @   s<   e Zd ZdZdeeef fddZede	dd fddZ
d	S )
DownloadEndParamsz%Parameters for the downloadEnd event.download_paramsc                 C   s
   || _ d S r%   )rh   )r&   rh   r   r   r   r'     s   
zDownloadEndParams.__init__r)   r*   c                 C   s>   | d}|dkr| t|S |dkr| t|S td)Nrc   rb   r   z.status must be either 'canceled' or 'complete')r/   ra   r5   re   r2   )r4   r)   rc   r   r   r   r5     s   
zDownloadEndParams.from_jsonN)r   r   r   r   r   ra   re   r'   r6   r7   r5   r   r   r   r   rg     s    

rg   c                   @   &   e Zd ZdZdZedefddZdS )ContextCreatedz5Event class for browsingContext.contextCreated event.browsingContext.contextCreatedr)   c                 C      t |tr|S t|S r%   r0   r8   r5   r4   r)   r   r   r   r5        

zContextCreated.from_jsonNr   r   r   r   event_classr6   r7   r5   r   r   r   r   rj     
    rj   c                   @   ri   )ContextDestroyedz7Event class for browsingContext.contextDestroyed event. browsingContext.contextDestroyedr)   c                 C   rl   r%   rm   rn   r   r   r   r5     ro   zContextDestroyed.from_jsonNrp   r   r   r   r   rs     rr   rs   c                   @   ri   )NavigationStartedz8Event class for browsingContext.navigationStarted event.!browsingContext.navigationStartedr)   c                 C   rl   r%   r0   r   r5   rn   r   r   r   r5     ro   zNavigationStarted.from_jsonNrp   r   r   r   r   ru     rr   ru   c                   @   ri   )NavigationCommittedz:Event class for browsingContext.navigationCommitted event.#browsingContext.navigationCommittedr)   c                 C   rl   r%   rw   rn   r   r   r   r5     ro   zNavigationCommitted.from_jsonNrp   r   r   r   r   rx     rr   rx   c                   @   ri   )NavigationFailedz7Event class for browsingContext.navigationFailed event. browsingContext.navigationFailedr)   c                 C   rl   r%   rw   rn   r   r   r   r5     ro   zNavigationFailed.from_jsonNrp   r   r   r   r   rz     rr   rz   c                   @   ri   )NavigationAbortedz8Event class for browsingContext.navigationAborted event.!browsingContext.navigationAbortedr)   c                 C   rl   r%   rw   rn   r   r   r   r5     ro   zNavigationAborted.from_jsonNrp   r   r   r   r   r|     rr   r|   c                   @   ri   )DomContentLoadedz7Event class for browsingContext.domContentLoaded event. browsingContext.domContentLoadedr)   c                 C   rl   r%   rw   rn   r   r   r   r5     ro   zDomContentLoaded.from_jsonNrp   r   r   r   r   r~     rr   r~   c                   @   ri   )Loadz+Event class for browsingContext.load event.browsingContext.loadr)   c                 C   rl   r%   rw   rn   r   r   r   r5     ro   zLoad.from_jsonNrp   r   r   r   r   r   
  rr   r   c                   @   ri   )FragmentNavigatedz8Event class for browsingContext.fragmentNavigated event.!browsingContext.fragmentNavigatedr)   c                 C   rl   r%   rw   rn   r   r   r   r5     ro   zFragmentNavigated.from_jsonNrp   r   r   r   r   r     rr   r   c                   @   ri   )DownloadWillBeginz8Event class for browsingContext.downloadWillBegin event.!browsingContext.downloadWillBeginr)   c                 C   
   t |S r%   )rG   r5   rn   r   r   r   r5   '     
zDownloadWillBegin.from_jsonNrp   r   r   r   r   r   "  rr   r   c                   @   ri   )UserPromptOpenedz7Event class for browsingContext.userPromptOpened event. browsingContext.userPromptOpenedr)   c                 C   r   r%   )rQ   r5   rn   r   r   r   r5   1  r   zUserPromptOpened.from_jsonNrp   r   r   r   r   r   ,  rr   r   c                   @   ri   )UserPromptClosedz7Event class for browsingContext.userPromptClosed event. browsingContext.userPromptClosedr)   c                 C   r   r%   )rY   r5   rn   r   r   r   r5   ;  r   zUserPromptClosed.from_jsonNrp   r   r   r   r   r   6  rr   r   c                   @   ri   )HistoryUpdatedz5Event class for browsingContext.historyUpdated event.browsingContext.historyUpdatedr)   c                 C   r   r%   )r_   r5   rn   r   r   r   r5   E  r   zHistoryUpdated.from_jsonNrp   r   r   r   r   r   @  rr   r   c                   @   ri   )DownloadEndz2Event class for browsingContext.downloadEnd event.browsingContext.downloadEndr)   c                 C   r   r%   )rg   r5   rn   r   r   r   r5   O  r   zDownloadEnd.from_jsonNrp   r   r   r   r   r   J  rr   r   c                   @   s&   e Zd ZU eed< eed< eed< dS )EventConfig	event_key
bidi_eventrq   N)r   r   r   r1   __annotations__rB   r   r   r   r   r   T  s   
 r   c                	   @   s   e Zd ZdZdeeef fddZdedefddZdd
ede	e
e  dd	fddZd
edd	fddZd
ededd	fddZd
ededd	fddZddedede	e
e  defddZdededd	fddZdddZd	S )_EventManagerzFClass to manage event subscriptions and callbacks for BrowsingContext.event_configsc                 C   sH   || _ || _i | _dd | D | _dt| | _t	
 | _d S )Nc                 S   s   i | ]}|j |jqS r   )r   rq   ).0configr   r   r   
<dictcomp>b      z*_EventManager.__init__.<locals>.<dictcomp>z, )connr   subscriptionsvalues_bidi_to_classjoinsortedkeys_available_events	threadingLock_subscription_lock)r&   r   r   r   r   r   r'   ^  s   z_EventManager.__init__eventr*   c                 C   s*   | j |}|std| d| j |S )NzEvent 'z' not found. Available events: )r   r/   r2   r   )r&   r   event_configr   r   r   validate_eventg  s   z_EventManager.validate_eventNr   contextsc                 C   sp   | j + || jvr&t| j}| j|j||d g | j|< W d   dS W d   dS 1 s1w   Y  dS )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   sessionr   r   r   subscribe_to_eventm  s   

"z _EventManager.subscribe_to_eventc                 C   s   | j 7 | j|}|dur*|s2t| j}| j|| | j|= W d   dS W d   dS W d   dS 1 s=w   Y  dS )zUnsubscribe from a BiDi event if no more callbacks exist.

        Parameters:
        ----------
            bidi_event: The BiDi event name.
        N)r   r   r/   r   r   r   unsubscribe)r&   r   callback_listr   r   r   r   unsubscribe_from_event{  s   

"z$_EventManager.unsubscribe_from_eventcallback_idc                 C   s<   | j  | j| | W d    d S 1 sw   Y  d S r%   )r   r   rC   )r&   r   r   r   r   r   add_callback_to_tracking  s   "z&_EventManager.add_callback_to_trackingc                 C   sn   | j * | j|}|r||v r%|| W d    d S W d    d S W d    d S 1 s0w   Y  d S r%   )r   r   r/   remove)r&   r   r   r   r   r   r   remove_callback_from_tracking  s   "z+_EventManager.remove_callback_from_trackingcallbackc                 C   s:   |  |}| j|j|}| |j| | |j| |S r%   )r   r   add_callbackrq   r   r   r   )r&   r   r   r   r   r   r   r   r   add_event_handler  s
   
z_EventManager.add_event_handlerc                 C   s8   |  |}| j|j| | |j| | |j d S r%   )r   r   remove_callbackrq   r   r   r   )r&   r   r   r   r   r   r   remove_event_handler  s   
z"_EventManager.remove_event_handlerc                 C   s   | j H | js	 W d   dS t| j}t| j D ]!\}}| j|}|r=|D ]	}| j|| q*| j	|
| q| j  W d   dS 1 sNw   Y  dS z3Clear all event handlers from the browsing context.N)r   r   r   r   rA   itemsr   r/   r   r   r   clear)r&   r   r   callback_idsrq   r   r   r   r   clear_event_handlers  s   
"z"_EventManager.clear_event_handlersr%   r*   N)r   r   r   r   r7   r1   r   r'   r   r   rA   r   r   r3   r   r   r   r   r   r   r   r   r   r   r   [  s    	 $r   c                   @   s
  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d  Zed!ee fd"d#Zd$ed!d%fd&d'Z	(	%	%did$ed)ed*ee d+ee d!ef
d,d-Zdjd$ed/ed!d%fd0d1Z	%	.	%dkd2ed3ee d4ed5ee d!ef
d6d7Z	%	%dld8ee  d9ee d!ee! fd:d;Z"	%	%dld$ed<ee d=ee d!d%fd>d?Z#	%	%	%dmd$ed@edAee  dBee dCeee  d!ee fdDdEZ$	%dnd$edFedGee d!efdHdIZ%	.	%	J	%	%	K	Ldod$ed4edMee dNedOee dPeee&e ef   dQe'dRed!efdSdTZ(	%	%dld$edUee dGee d!efdVdWZ)	%	%	%	%dpd$ee d(ee dXee' dYeee  d!d%f
dZd[Z*d$ed\e d!efd]d^Z+dnd_ed`e,daeee  d!e fdbdcZ-d_edde d!d%fdedfZ.dqdgdhZ/d%S )rBrowsingContextz2BiDi implementation of the browsingContext module.context_createdrk   context_destroyedrt   dom_content_loadedr   download_endr   download_will_beginr   fragment_navigatedr   history_updatedr   loadr   navigation_abortedr}   navigation_committedry   navigation_failedr{   navigation_startedrv   user_prompt_closedr   user_prompt_openedr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   c                 C   s   || _ t|| j| _d S r%   )r   r   EVENT_CONFIGS_event_manager)r&   r   r   r   r   r'     s   zBrowsingContext.__init__r*   c                 C   s   t | 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.
        )rA   r   r   )r4   r   r   r   get_event_names  s   zBrowsingContext.get_event_namesr    Nc                 C   s   d|i}| j td| dS )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    paramsr   r   r   activate  s   zBrowsingContext.activateviewportoriginformatclipc                 C   sD   ||d}|dur||d< |dur||d< | 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   Nr   r   z!browsingContext.captureScreenshotdatar   )r&   r    r   r   r   r   resultr   r   r   capture_screenshot  s   
z"BrowsingContext.capture_screenshotFprompt_unloadc                 C   s    ||d}| j td| dS )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   r   r   r   close  s   
zBrowsingContext.closerB   reference_context
backgroundr;   c                 C   sR   d|i}|dur||d< |dur||d< |dur||d< | 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.
        rB   NreferenceContextr   r>   zbrowsingContext.creater    r   )r&   rB   r   r   r;   r   r   r   r   r   create   s   zBrowsingContext.create	max_depthrootc                 C   sH   i }|dur
||d< |dur||d< | j td|}dd |d D S )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.
        NmaxDepthr   zbrowsingContext.getTreec                 S   s   g | ]}t |qS r   )r8   r5   )r   r    r   r   r   
<listcomp>W  r   z,BrowsingContext.get_tree.<locals>.<listcomp>r   r   )r&   r   r   r   r   r   r   r   get_tree?  s   zBrowsingContext.get_treeacceptr[   c                 C   s>   d|i}|dur||d< |dur||d< | j td| dS )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   r]   z browsingContext.handleUserPromptr   )r&   r    r   r[   r   r   r   r   handle_user_promptY  s   z"BrowsingContext.handle_user_promptlocatormax_node_countserialization_optionsstart_nodesc                 C   sT   ||d}|dur||d< |dur||d< |dur||d< | 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   NmaxNodeCountserializationOptions
startNodeszbrowsingContext.locateNodesnodesr   )r&   r    r   r   r   r   r   r   r   r   r   locate_nodeso  s   
zBrowsingContext.locate_nodesr#   waitc                 C   s0   ||d}|dur||d< | 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#   Nr   zbrowsingContext.navigater   )r&   r    r#   r   r   r   r   r   r   navigate  s
   
zBrowsingContext.navigateportrait      ?Tmarginorientationpagepage_rangesscaleshrink_to_fitc	                 C   sZ   |||||d}	|dur||	d< |dur||	d< |dur ||	d< | 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   shrinkToFitNr   r   
pageRangeszbrowsingContext.printr   r   )r&   r    r   r   r   r   r   r   r   r   r   r   r   r   print  s   zBrowsingContext.printignore_cachec                 C   s>   d|i}|dur||d< |dur||d< | 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    NignoreCacher   zbrowsingContext.reloadr   )r&   r    r   r   r   r   r   r   r   reload  s   zBrowsingContext.reloaddevice_pixel_ratiouser_contextsc                 C   sZ   i }|dur
||d< |dur||d< |dur||d< |dur"||d< | j td| dS )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  r   r   r   r   set_viewport  s   zBrowsingContext.set_viewportdeltac                 C   s    ||d}| 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  r   r   r   r   r   traverse_history  s   
z BrowsingContext.traverse_historyr   r   r   c                 C   s   | 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   r   r   r   r   "  s   z!BrowsingContext.add_event_handlerr   c                 C   s   | j || dS )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   r   r   r   r   1  s   z$BrowsingContext.remove_event_handlerc                 C   s   | j   dS r   )r   r   )r&   r   r   r   r   ;  s   z$BrowsingContext.clear_event_handlers)r   NN)F)NFNrF   )NNNr%   )FNr   NNr   T)NNNNr   )0r   r   r   r   r   rj   rs   r~   r   r   r   r   r   r|   rx   rz   ru   r   r   r   r'   r6   rA   r1   r   r   r   r7   r   r^   r   r   r3   r8   r   r   r   r   r   floatr   r  r  r	  r   r   r   r   r   r   r   r   r     s8   











	

!



%
	

1


 $
r   )(r   dataclassesr   typingr   r   r   r   %selenium.webdriver.common.bidi.commonr   &selenium.webdriver.common.bidi.sessionr   r	   r   r   r8   rG   rQ   rY   r_   ra   re   rg   rj   rs   ru   rx   rz   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sD   	.P:3,$




e