o
    ѷ6i                     @   s  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
 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/d0 d0Z!G d1d2 d2Z"G d3d4 d4Z#G d5d6 d6Z$G d7d8 d8Z%G d9d: d:Z&eG d;d< d<Z'G d=d> d>Z(G d?d@ d@Z)dS )A    N)Callable)	dataclass)Any)Sentinel)command_builder)Session	UNDEFINEDc                   @   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   t/var/www/hoanhtaovolam_webdjango/env/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dB 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
navigationN	timestampurlc                 C      || _ || _|| _|| _d S N)r    r!   r"   r#   )selfr    r!   r"   r#   r   r   r   __init__3      
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 )zCreates a NavigationInfo instance from a dictionary.

        Args:
            json: A dictionary containing the navigation information.

        Returns:
            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_json
r   r   r   r   r1   r3   r'   classmethoddictr5   r   r   r   r   r   0   s    
r   c                   @   sb   e Zd ZdZ		ddededed  dB dedededB d	edB 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#   r:   r=   r<   r>   r;   )r&   r    r#   r:   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 )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    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#   r:   r;   r<   r=   r>   )
r/   r0   listr2   r8   typeappendr9   r5   r1   )r4   r)   r:   raw_childrenchildr    r#   r=   r<   r>   r;   r   r   r   r5   q   sJ   









zBrowsingContextInfo.from_jsonNN)
r   r   r   r   r1   rB   r'   r7   r8   r5   r   r   r   r   r9   \   s*    	

r9   c                
       sP   e Zd ZdZdededB 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!   Nr"   r#   suggested_filenamec                       t  |||| || _d S r%   )superr'   rI   )r&   r    r!   r"   r#   rI   	__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#   rI   )
r   r5   r/   r0   r1   r2   r    r!   r"   r#   )r4   r)   nav_inforI   r   r   r   r5      s   

z!DownloadWillBeginParams.from_json)r   r   r   r   r1   r3   r'   r7   r8   r5   __classcell__r   r   rL   r   rH      s    rH   c                   @   sL   e Zd ZdZ	ddedededededB f
dd	Zed
edd fddZdS )UserPromptOpenedParamsz*Parameters for the userPromptOpened event.Nr    handlermessagerC   default_valuec                 C   s"   || _ || _|| _|| _|| _d S r%   r    rS   rT   rC   rU   )r&   r    rS   rT   rC   rU   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 )zCreates a UserPromptOpenedParams instance from a dictionary.

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

        Returns:
            A new instance of UserPromptOpenedParams.
        r    Nr+   rS   z(handler is required and must be a stringrT   z(message is required and must be a stringrC   %type is required and must be a stringdefaultValuez)defaultValue must be a string if providedrV   )r/   r0   r1   r2   )r4   r)   r    rS   rT   
type_valuerU   r   r   r   r5      s,   





z UserPromptOpenedParams.from_jsonr%   )	r   r   r   r   r1   r'   r7   r8   r5   r   r   r   r   rR      s     
rR   c                
   @   sH   e Zd ZdZ	ddededededB fddZed	ed
d fddZ	dS )UserPromptClosedParamsz*Parameters for the userPromptClosed event.Nr    acceptedrC   	user_textc                 C   r$   r%   r    r[   rC   r\   )r&   r    r[   rC   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 )zCreates a UserPromptClosedParams instance from a dictionary.

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

        Returns:
            A new instance of UserPromptClosedParams.
        r    Nr+   r[   z*accepted is required and must be a booleanrC   rW   userTextz%userText must be a string if providedr]   )r/   r0   r1   r2   bool)r4   r)   r    r[   rY   r\   r   r   r   r5     s$   




z UserPromptClosedParams.from_jsonr%   )
r   r   r   r   r1   r_   r'   r7   r8   r5   r   r   r   r   rZ     s    
rZ   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'   5  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 )
zCreates a HistoryUpdatedParams instance from a dictionary.

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

        Returns:
            A new instance of HistoryUpdatedParams.
        r    Nr+   r"   r   r,   r#   r-   ra   r.   )r4   r)   r    r"   r#   r   r   r   r5   ?  s   



zHistoryUpdatedParams.from_jsonNr6   r   r   r   r   r`   2  s    

r`   c                       sP   e Zd Z	ddededB 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!   Nr"   r#   statusc                    rJ   r%   )rK   r'   rd   )r&   r    r!   r"   r#   rd   rL   r   r   r'   ]  rN   zDownloadCanceledParams.__init__r)   r*   c                 C   sF   t |}|d}|d u s|dkrtd| |j|j|j|j|dS )Nrd   rc   z)status is required and must be 'canceled')r    r!   r"   r#   rd   )r   r5   r/   r2   r    r!   r"   r#   )r4   r)   rP   rd   r   r   r   r5   h  s   

z DownloadCanceledParams.from_json)rc   
r   r   r   r1   r3   r'   r7   r8   r5   rQ   r   r   rL   r   rb   \  s    rb   c                       sZ   e Zd Z		ddededB dededededB f fd	d
Zededd fddZ  Z	S )DownloadCompleteParamsr   Nr    r!   r"   r#   rd   filepathc                    s"   t  |||| || _|| _d S r%   )rK   r'   rd   rg   )r&   r    r!   r"   r#   rd   rg   rL   r   r   r'   z  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 )Nrd   r   z)status is required and must be 'complete'rg   z%filepath must be a string if provided)r    r!   r"   r#   rd   rg   )
r   r5   r/   r2   r0   r1   r    r!   r"   r#   )r4   r)   rP   rd   rg   r   r   r   r5     s   


z DownloadCompleteParams.from_json)r   Nre   r   r   rL   r   rf   y  s$    rf   c                   @   s8   e Zd ZdZdeeB 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%   )ri   )r&   ri   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)Nrd   rc   r   z.status must be either 'canceled' or 'complete')r/   rb   r5   rf   r2   )r4   r)   rd   r   r   r   r5     s   
zDownloadEndParams.from_jsonN)
r   r   r   r   rb   rf   r'   r7   r8   r5   r   r   r   r   rh     s    
rh   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   r9   r5   r4   r)   r   r   r   r5        

zContextCreated.from_jsonNr   r   r   r   event_classr7   r8   r5   r   r   r   r   rk     
    rk   c                   @   rj   )ContextDestroyedz7Event class for browsingContext.contextDestroyed event. browsingContext.contextDestroyedr)   c                 C   rm   r%   rn   ro   r   r   r   r5     rp   zContextDestroyed.from_jsonNrq   r   r   r   r   rt     rs   rt   c                   @   rj   )NavigationStartedz8Event class for browsingContext.navigationStarted event.!browsingContext.navigationStartedr)   c                 C   rm   r%   r0   r   r5   ro   r   r   r   r5     rp   zNavigationStarted.from_jsonNrq   r   r   r   r   rv     rs   rv   c                   @   rj   )NavigationCommittedz:Event class for browsingContext.navigationCommitted event.#browsingContext.navigationCommittedr)   c                 C   rm   r%   rx   ro   r   r   r   r5     rp   zNavigationCommitted.from_jsonNrq   r   r   r   r   ry     rs   ry   c                   @   rj   )NavigationFailedz7Event class for browsingContext.navigationFailed event. browsingContext.navigationFailedr)   c                 C   rm   r%   rx   ro   r   r   r   r5     rp   zNavigationFailed.from_jsonNrq   r   r   r   r   r{     rs   r{   c                   @   rj   )NavigationAbortedz8Event class for browsingContext.navigationAborted event.!browsingContext.navigationAbortedr)   c                 C   rm   r%   rx   ro   r   r   r   r5     rp   zNavigationAborted.from_jsonNrq   r   r   r   r   r}     rs   r}   c                   @   rj   )DomContentLoadedz7Event class for browsingContext.domContentLoaded event. browsingContext.domContentLoadedr)   c                 C   rm   r%   rx   ro   r   r   r   r5     rp   zDomContentLoaded.from_jsonNrq   r   r   r   r   r     rs   r   c                   @   rj   )Loadz+Event class for browsingContext.load event.browsingContext.loadr)   c                 C   rm   r%   rx   ro   r   r   r   r5   
  rp   zLoad.from_jsonNrq   r   r   r   r   r     rs   r   c                   @   rj   )FragmentNavigatedz8Event class for browsingContext.fragmentNavigated event.!browsingContext.fragmentNavigatedr)   c                 C   rm   r%   rx   ro   r   r   r   r5     rp   zFragmentNavigated.from_jsonNrq   r   r   r   r   r     rs   r   c                   @   rj   )DownloadWillBeginz8Event class for browsingContext.downloadWillBegin event.!browsingContext.downloadWillBeginr)   c                 C   
   t |S r%   )rH   r5   ro   r   r   r   r5   "     
zDownloadWillBegin.from_jsonNrq   r   r   r   r   r     rs   r   c                   @   rj   )UserPromptOpenedz7Event class for browsingContext.userPromptOpened event. browsingContext.userPromptOpenedr)   c                 C   r   r%   )rR   r5   ro   r   r   r   r5   ,  r   zUserPromptOpened.from_jsonNrq   r   r   r   r   r   '  rs   r   c                   @   rj   )UserPromptClosedz7Event class for browsingContext.userPromptClosed event. browsingContext.userPromptClosedr)   c                 C   r   r%   )rZ   r5   ro   r   r   r   r5   6  r   zUserPromptClosed.from_jsonNrq   r   r   r   r   r   1  rs   r   c                   @   rj   )HistoryUpdatedz5Event class for browsingContext.historyUpdated event.browsingContext.historyUpdatedr)   c                 C   r   r%   )r`   r5   ro   r   r   r   r5   @  r   zHistoryUpdated.from_jsonNrq   r   r   r   r   r   ;  rs   r   c                   @   rj   )DownloadEndz2Event class for browsingContext.downloadEnd event.browsingContext.downloadEndr)   c                 C   r   r%   )rh   r5   ro   r   r   r   r5   J  r   zDownloadEnd.from_jsonNrq   r   r   r   r   r   E  rs   r   c                   @   s&   e Zd ZU eed< eed< eed< dS )EventConfig	event_key
bidi_eventrr   N)r   r   r   r1   __annotations__rC   r   r   r   r   r   O  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 d	B 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 d	B 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   rr   ).0configr   r   r   
<dictcomp>]      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'   Y  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_eventb  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.

        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   sessionr   r   r   subscribe_to_eventh  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 )z~Unsubscribe from a BiDi event if no more callbacks exist.

        Args:
            bidi_event: The BiDi event name.
        N)r   r   r/   r   r   r   unsubscribe)r&   r   callback_listr   r   r   r   unsubscribe_from_eventu  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   rD   )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_callbackrr   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_callbackrr   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   rB   itemsr   r/   r   r   r   clear)r&   r   r   callback_idsrr   r   r   r   r   clear_event_handlers  s   
"z"_EventManager.clear_event_handlersr%   r*   N)r   r   r   r   r8   r1   r   r'   r   rB   r   r   r3   r   r   r   r   r   r   r   r   r   r   r   V  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d%B d+ed%B d!ef
d,d-Zdjd$ed/ed!d%fd0d1Z	%	.	%dkd2ed3ed%B d4ed5ed%B d!ef
d6d7Z	%	%dld8ed%B d9ed%B d!ee  fd:d;Z!	%	%dld$ed<ed%B d=ed%B d!d%fd>d?Z"	%	%	%dmd$ed@edAed%B dBed%B dCee d%B d!ee fdDdEZ#	%dnd$edFedGed%B d!efdHdIZ$	.	%	J	%	%	K	Ldod$ed4edMed%B dNedOed%B dPeeeB  d%B dQe%dRed!efdSdTZ&	%	%dld$edUed%B dGed%B d!efdVdWZ'd%e(e(d%fd$ed%B d(ed%B e(B dXe%d%B e(B dYee d%B d!d%f
dZd[Z)d$ed\ed!efd]d^Z*dnd_ed`e+daee d%B d!efdbdcZ,d_edded!d%fdedfZ-dpdgdhZ.d%S )qBrowsingContextz2BiDi implementation of the browsingContext module.context_createdrl   context_destroyedru   dom_content_loadedr   download_endr   download_will_beginr   fragment_navigatedr   history_updatedr   loadr   navigation_abortedr~   navigation_committedrz   navigation_failedr|   navigation_startedrw   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:
            A list of event names that can be used with event handlers.
        )rB   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 )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    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.

        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   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.

        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   r   r   r   close  s   

zBrowsingContext.closerC   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.

        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.
        rC   NreferenceContextr   r?   zbrowsingContext.creater    r   )r&   rC   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  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.
        NmaxDepthr   zbrowsingContext.getTreec                 S   s   g | ]}t |qS r   )r9   r5   )r   r    r   r   r   
<listcomp>G  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.

        Args:
            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_promptI  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.

        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    r   NmaxNodeCountserializationOptions
startNodeszbrowsingContext.locateNodesnodesr   )r&   r    r   r   r   r   r   r   r   r   r   locate_nodes^  s   
zBrowsingContext.locate_nodesr#   waitc                 C   s0   ||d}|dur||d< | 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#   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,  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   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 )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    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   s   |dur|durt d|du r|du rt di }|dur#||d< n|dur+||d< |tur3||d< |tur;||d< | jtd| dS )	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)r2   r   r   r   r   )r&   r    r   r  r  r   r   r   r   set_viewport  s   
zBrowsingContext.set_viewportdeltac                 C   s    ||d}| 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    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 )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   r   r   r   r     s   z!BrowsingContext.add_event_handlerr   c                 C   s   | j || dS )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   r   r   r   r     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)NFNrG   )NNNr%   )FNr   NNr   Tr   )/r   r   r   r   r   rk   rt   r   r   r   r   r   r   r}   ry   r{   rv   r   r   r   r'   r7   rB   r1   r   r   r8   r   r_   r   r   r3   r9   r   r   r   r   floatr   r  r   r  r
  r   r   r   r   r   r   r   r   r     s8   

















#
	

.




&$	r   )*r   collections.abcr   dataclassesr   typingr   typing_extensionsr   %selenium.webdriver.common.bidi.commonr   &selenium.webdriver.common.bidi.sessionr   r   r	   r   r   r9   rH   rR   rZ   r`   rb   rf   rh   rk   rt   rv   ry   r{   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sJ   	,N81*$




c