a
    7ñ»j°«  ã                   @  sú   d dl mZ d dlZd dlZd dlZd dlZ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Zd dlZd dlZd dlZd dlZd dlZe e¡Zejr°ddlmZ dd	d
ddœdd„ZG dd„ dƒZG dd„ dejj ƒZ!d
ddœdd„Z"dS )é    )ÚannotationsN)Úsuppressé   )Úcdp_util)Ú
ContraDict)ÚPathLike)ÚTabúcdp.dom.Noder   ztyping.Optional[cdp.dom.Node]©ÚnodeÚtabÚtreec                 C  s   t | ||ƒ}|S )a!  
    Factory for Elements.
    This is used with Tab.query_selector(_all).
    Since we already have the tree,
    we don't need to fetch it for every single element.
    :param node: cdp dom node representation
    :type node: cdp.dom.Node
    :param tab: the target object to which this element belongs
    :type tab: Tab
    :param tree: [Optional] the full node tree to which <node> belongs,
        enhances performance. When not provided, you need to
        call `await elem.update()` before using .children / .parent
    :type tree:
    )ÚElement)r   r   r   Úelem© r   úPE:\soccer\ml_env\Lib\site-packages\seleniumbase/undetected/cdp_driver/element.pyÚcreate   s    r   c                      sþ  e Zd ZdÊddddœdd„Zedd„ ƒZed	d
„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd „ ƒZed!d"„ ƒZed#d$„ ƒZed%d&„ ƒZed'd(„ ƒZed)d*„ ƒZed+d,„ ƒZed-d.„ ƒZed/d0„ ƒZed1d2„ ƒZed3d4„ ƒZed5d6„ ƒZed7d8„ ƒZed9d:„ ƒZed;d<„ ƒZed=d>„ ƒZ ed?d@„ ƒZ!edAdB„ ƒZ"edCdD„ ƒZ#edEdF„ ƒZ$dGdH„ Z%‡ fdIdJ„Z&dKdL„ Z'dMdN„ Z(dOdP„ Z)dQdR„ Z*dËdSdT„Z+edUdV„ ƒZ,eddWœdXdY„ƒZ-e-j.ddZœd[dY„ƒZ-ed\d]„ ƒZ/ed^dWœd_d`„ƒZ0edadWœdbdc„ƒZ1edddWœdedf„ƒZ2edgdWœdhdi„ƒZ3djdk„ Z4dldm„ Z5dndo„ Z6dpdq„ Z7dÌdsdt„Z8dÍdvdWœdwdx„Z9dÎd|d}d}d~dœd€d�„Z:dÏd‚d‚d~d|d}d}dƒœd„d…„Z;d†d‡„ Z<dÐdˆd~d‰dŠœd‹dŒ„Z=d�dŽ„ Z>d�d�„ Z?d‘d’„ Z@d|d“œd”d•„ZAd–d—œd˜d™„ZBdšd›„ ZCdœd�„ ZDdždŸ„ ZEd d¡„ ZFd¢d£„ ZGed|dWœd¤d¥„ƒZHed¦d§„ ƒZIed¨d©„ ƒZJd|dªœd«d¬„ZKd|dªœd­d®„ZLdÑd±d²d³d´œdµd¶„ZMdÒd‚d²d‚d‚d¹œdºd»„ZNd¼d½„ ZOdÓd²d²d³d¾œd¿dÀ„ZPdÁdÂ„ ZQdÃdÄ„ ZRd d~dÅœdÆdÇ„ZSdÈdÉ„ ZT‡  ZUS )Ôr   Nr	   r   r
   c                 C  sB   |st dƒ‚|| _|| _|| _d| _d| _tdd�| _|  ¡  dS )zß
        Represents an (HTML) DOM Element
        :param node: cdp dom node representation
        :type node: cdp.dom.Node
        :param tab: the target object to which this element belongs
        :type tab: Tab
        zNode cannot be None!NT)Zsilent)	Ú	ExceptionÚ_tabÚ_nodeÚ_treeÚ_parentÚ_remote_objectr   Ú_attrsÚ_make_attrs)Úselfr   r   r   r   r   r   Ú__init__/   s    
zElement.__init__c                 C  s   | j r| j  ¡ S d S ©N)Ú	node_nameÚlower©r   r   r   r   ÚtagE   s    zElement.tagc                 C  s   | j S r   )r!   r    r   r   r   Útag_nameJ   s    zElement.tag_namec                 C  s   | j jS r   )r   Únode_idr    r   r   r   r#   N   s    zElement.node_idc                 C  s   | j jS r   )r   Úbackend_node_idr    r   r   r   r$   R   s    zElement.backend_node_idc                 C  s   | j jS r   )r   Ú	node_typer    r   r   r   r%   V   s    zElement.node_typec                 C  s   | j jS r   )r   r   r    r   r   r   r   Z   s    zElement.node_namec                 C  s   | j jS r   )r   Ú
local_namer    r   r   r   r&   ^   s    zElement.local_namec                 C  s   | j jS r   )r   Ú
node_valuer    r   r   r   r'   b   s    zElement.node_valuec                 C  s   | j jS r   )r   Ú	parent_idr    r   r   r   r(   f   s    zElement.parent_idc                 C  s   | j jS r   )r   Úchild_node_countr    r   r   r   r)   j   s    zElement.child_node_countc                 C  s   | j jS r   )r   Ú
attributesr    r   r   r   r*   n   s    zElement.attributesc                 C  s   | j jS r   )r   Údocument_urlr    r   r   r   r+   r   s    zElement.document_urlc                 C  s   | j jS r   )r   Úbase_urlr    r   r   r   r,   v   s    zElement.base_urlc                 C  s   | j jS r   )r   Ú	public_idr    r   r   r   r-   z   s    zElement.public_idc                 C  s   | j jS r   )r   Ú	system_idr    r   r   r   r.   ~   s    zElement.system_idc                 C  s   | j jS r   )r   Úinternal_subsetr    r   r   r   r/   ‚   s    zElement.internal_subsetc                 C  s   | j jS r   )r   Úxml_versionr    r   r   r   r0   †   s    zElement.xml_versionc                 C  s   | j jS r   )r   Úvaluer    r   r   r   r1   Š   s    zElement.valuec                 C  s   | j jS r   )r   Úpseudo_typer    r   r   r   r2   Ž   s    zElement.pseudo_typec                 C  s   | j jS r   )r   Úpseudo_identifierr    r   r   r   r3   ’   s    zElement.pseudo_identifierc                 C  s   | j jS r   )r   Úshadow_root_typer    r   r   r   r4   –   s    zElement.shadow_root_typec                 C  s   | j jS r   )r   Úframe_idr    r   r   r   r5   š   s    zElement.frame_idc                 C  s   | j jS r   )r   Úcontent_documentr    r   r   r   r6   ž   s    zElement.content_documentc                 C  s   | j jS r   )r   Úshadow_rootsr    r   r   r   r7   ¢   s    zElement.shadow_rootsc                 C  s   | j jS r   )r   Útemplate_contentr    r   r   r   r8   ¦   s    zElement.template_contentc                 C  s   | j jS r   )r   Úpseudo_elementsr    r   r   r   r9   ª   s    zElement.pseudo_elementsc                 C  s   | j jS r   )r   Úimported_documentr    r   r   r   r:   ®   s    zElement.imported_documentc                 C  s   | j jS r   )r   Údistributed_nodesr    r   r   r   r;   ²   s    zElement.distributed_nodesc                 C  s   | j jS r   )r   Úis_svgr    r   r   r   r<   ¶   s    zElement.is_svgc                 C  s   | j jS r   )r   Úcompatibility_moder    r   r   r   r=   º   s    zElement.compatibility_modec                 C  s   | j jS r   )r   Úassigned_slotr    r   r   r   r>   ¾   s    zElement.assigned_slotc                 C  s   | j S r   )r   r    r   r   r   r   Â   s    zElement.tabc                 C  s   t | j|d ƒ}|r|S d S r   )ÚgetattrÚattrs)r   ÚitemÚxr   r   r   Ú__getattr__Æ   s    zElement.__getattr__c                   sH   |d dkr6|dd … t | ƒ ¡ vr6| j ||¡ d S tƒ  ||¡ d S ©Nr   Ú_r   )ÚvarsÚkeysr@   Ú__setattr__Úsuper©r   Úkeyr1   ©Ú	__class__r   r   rH   Ð   s
    zElement.__setattr__c                 C  s2   |d dkr.|dd … t | ƒ ¡ vr.|| j|< d S rD   )rF   rG   r@   rJ   r   r   r   Ú__setitem__×   s    zElement.__setitem__c                 C  s   | j  |d ¡S r   )r@   Úget)r   rA   r   r   r   Ú__getitem__Ü   s    zElement.__getitem__c                 Ã  sV   | j  tjj| jd�¡I dH | _| j  tjj| jt	| ƒd�¡I dH  |  
¡ I dH  dS )zSaves element to DOM.©r$   N)Z
outer_html)r   ÚsendÚcdpÚdomÚresolve_noder$   r   Zset_outer_htmlr#   ÚstrÚupdater    r   r   r   Úsave_to_dom_asyncß   s    ÿÿ
zElement.save_to_dom_asyncc                 ƒ  sH   ˆ   ¡ I dH  t ˆ j‡ fdd„¡}|rDˆ j tj |j	¡¡I dH  dS )zRemoves element from DOM.Nc                   s   | j ˆ j kS r   rQ   )r   r    r   r   Ú<lambda>î   ó    z/Element.remove_from_dom_async.<locals>.<lambda>)
rW   ÚutilÚfilter_recurser   r   rR   rS   rT   Úremove_noder#   )r   r   r   r    r   Úremove_from_dom_asyncé   s    
þzElement.remove_from_dom_asyncc                 ƒ  sÈ   |r|}dˆ _ n"ˆ j tj dd¡¡I dH }dˆ _ t |‡ fdd„¡}|rZt 	d¡ |ˆ _
|ˆ _ˆ j tjjˆ j
jd�¡I dH ˆ _ˆ  ¡  ˆ jdkrÄt |‡ fd	d„¡}|s°ˆ S t|ˆ jˆ jd
�ˆ _ ˆ S )aõ  
        Updates element to retrieve more properties.
        For example this enables:
        :py:obj:`~children` and :py:obj:`~parent` attributes.
        Also resolves js object,
        which is a stored object in :py:obj:`~remote_object`.
        Usually you will get element nodes by the usage of:
        :py:meth:`Tab.query_selector_all()`
        :py:meth:`Tab.find_elements_by_text()`
        Those elements are already updated and you can browse
        through children directly.
        NéÿÿÿÿTc                   s   | j ˆ jj kS r   )r$   r   ©Únr    r   r   rY     rZ   z Element.update.<locals>.<lambda>z'Node changed, and has now been updated.rQ   ÚIFRAMEc                   s   | j ˆ jjkS r   )r#   r   r(   r`   r    r   r   rY     rZ   ©r   r   )r   r   rR   rS   rT   Zget_documentr[   r\   ÚloggerÚdebugr   r   rU   r$   r   r   r   r   )r   r   ÚdocZupdated_nodeÚparent_noder   r    r   rW   ô   s0    ÿ
ÿ
ÿzElement.updatec                 C  s   | j S r   ©r   r    r   r   r   r      s    zElement.node)Úreturnc                 C  s   | j S r   ©r   r    r   r   r   r   $  s    zElement.tree)r   c                 C  s
   || _ d S r   rj   )r   r   r   r   r   r   (  s    c                 C  s   | j S )zk
        Attributes are stored here.
        You can also set them directly on the element object.
        )r   r    r   r   r   r@   ,  s    zElement.attrsztyping.Union[Element, None]c                   sB   ˆ j stdƒ‚t ˆ j ‡ fdd„¡}|s,dS t|ˆ jˆ j d�}|S )z8Get the parent element (node) of current element (node).z7Could not get parent since the element has no tree set.c                   s   | j ˆ jkS r   )r#   r(   r`   r    r   r   rY   <  rZ   z Element.parent.<locals>.<lambda>Nrc   )r   ÚRuntimeErrorr[   r\   r   r   )r   rg   Zparent_elementr   r    r   Úparent4  s    ÿÿzElement.parentz'typing.Union[typing.List[Element], str]c                 C  s’   g }| j jdkrN| j j}|js"g S |jD ] }t|| j|ƒ}|r(| |¡ q(|S | jjsZg S | jjrŽ| jjD ]"}t|| j| j	ƒ}|rj| |¡ qj|S )z¦
        Returns the element's children.
        Those children also have a children property
        so that you can browse through the entire tree as well.
        rb   )
r   r   r6   r)   Úchildrenr   r   Úappendr   r   )r   Z	_childrenÚframeÚchildZ
child_elemr   r   r   rm   C  s$    
zElement.childrenzcdp.runtime.RemoteObjectc                 C  s   | j S r   )r   r    r   r   r   Úremote_objectb  s    zElement.remote_objectzcdp.runtime.RemoteObjectIdc                 C  s"   z
| j jW S  ty   Y n0 d S r   )rq   Ú	object_idÚAttributeErrorr    r   r   r   rr   f  s    
zElement.object_idc              
   Ã  sŒ   | j  tjj| jd�¡I dH | _tjj| jj	d�g}d}| j
 |¡I dH }|s^|  d¡I dH  | j  tjjd| jj	|dddd�¡I dH  dS )	zClick the element.rQ   N©rr   ú)sessionStorage.getItem("pxsid") !== null;ç      Ð?z(el) => el.click()T)rr   Ú	argumentsÚawait_promiseÚuser_gestureÚreturn_by_value)r   rR   rS   rT   rU   r$   r   ÚruntimeÚCallArgumentrr   r   ÚevaluateÚflash_asyncÚcall_function_on)r   rw   ÚscriptÚusing_pxr   r   r   Úclick_asyncm  s,    ÿÿÿúÿzElement.click_asyncc                 Ã  s   t |  d¡I d H ƒS )NzÝ
                function (e) {
                    let o = {}
                    for(let k in e){
                        o[k] = e[k]
                    }
                    return o
                }
                )r   Úapplyr    r   r   r   Úget_js_attributes_async†  s
    ÿÿzElement.get_js_attributes_asyncc                 C  s   |   ¡  ¡ S r   )rW   Ú	__await__r    r   r   r   r…   •  s    zElement.__await__c                 C  s   |   d|› d�¡S )Nz
(e) => e['z']()©rƒ   )r   Z	js_methodr   r   r   Ú__call__˜  s    zElement.__call__Tc              	   Ã  s°   | j  tjj| jd�¡I dH | _| j  tjj|| jj	tjj
| jj	d�gddd�¡I dH }z<|r€|d r€|rv|d jW S |d W S |d r’|d W S W n tyª   |  Y S 0 dS )a5  
        Apply javascript to this element.
        The given js_function string should accept the js element as parameter,
        and can be a arrow function, or function declaration.
        Eg:
            - '(elem) => {
                    elem.value = "blabla"; consolelog(elem);
                    alert(JSON.stringify(elem);
                } '
            - 'elem => elem.play()'
            - function myFunction(elem) { alert(elem) }
        :param js_function: JS function definition which received this element.
        :param return_by_value:
        rQ   Nrt   T)rr   rw   rz   ry   r   r   )r   rR   rS   rT   rU   r$   r   r{   r   rr   r|   r1   r   )r   Zjs_functionrz   Úresultr   r   r   rƒ   ›  s2    ÿÿÿ÷ÿÿ
zElement.applyFÚPositionc                 Ã  sð   | j r| js,| j tjj| jd�¡I d H | _zž| j	 tjj
| jjd�¡I d H }|s^td|  ƒ‚t|d ƒ}|rÆ| j	 d¡I d H }| j	 d¡I d H }|j| |jd  }|j| |jd  }||_||_|W S  tyê   t d|  ¡ Y n0 d S )	NrQ   rt   zCould not find position for %s r   zwindow.scrollYzwindow.scrollXé   zPNo content quads for %s. Mostly caused by element which is not 'in plain sight'.)rl   rr   r   rR   rS   rT   rU   r$   r   r   Zget_content_quadsrq   r   r‰   r}   ÚleftÚwidthÚtopÚheightÚabs_xÚabs_yÚ
IndexErrorrd   re   )r   ÚabsZquadsÚposZscroll_yZscroll_xr�   r�   r   r   r   Úget_position_asyncÆ  s6    ÿÿÿ
þÿzElement.get_position_asyncr‹   r   r   rV   ztyping.Optional[int]Úbool)ÚbuttonÚbuttonsÚ	modifiersÚholdc                 Ã  sê   z|   ¡ I dH j}W n ty(   Y dS 0 |s>t d| ¡ dS t d| ¡ d}| j |¡I dH }|svt 	|  
d¡¡ t 	| j tjjd|d |d |tj |¡|dd	�¡¡ t 	| j tjjd
|d |d |tj |¡|dd	�¡¡ dS )a<  
        Native click (on element).
        :param button: str (default = "left")
        :param buttons: which button (default 1 = left)
        :param modifiers: *(Optional)*
                Bit field representing pressed modifier keys.
                Alt=1, Ctrl=2, Meta/Command=4, Shift=8 (default: 0).
        Nú$Could not calculate box model for %sú Clicking on location: %.2f, %.2fru   rv   ÚmousePressedr   r   ©rB   Úyr˜   r–   r—   Zclick_countÚmouseReleased)r”   Úcenterrs   rd   Úwarningre   r   r}   ÚasyncioÚcreate_taskr~   r   rR   rS   Úinput_Údispatch_mouse_eventÚMouseButton)r   r–   r—   r˜   r™   r    r€   r�   r   r   r   Úmouse_click_asyncä  sL    
ùÿÿ
ùÿÿzElement.mouse_click_asyncztyping.Union[float, int])rB   rž   r    r–   r—   r˜   c                 Ã  sd  t |ƒ}t |ƒ}z:|  ¡ I d H }	|	j}
|	j}|	j}|	j}|  ¡ I d H j}W n ty^   Y d S 0 |stt 	d| ¡ d S |r |d }|d }d}
d}t 
d| ¡ nt 
d||f ¡ d}| j |¡I d H }|sðt | j||
d  ||d  d�¡ t | j tjjd|| || |tj |¡|dd	�¡¡ t | j tjjd
|| || |tj |¡|dd	�¡¡ d S )Nrš   r   r   r›   ru   rŠ   )Úx_offsetÚy_offsetrœ   r�   rŸ   )Úintr”   rŒ   rŽ   r‹   r�   r    rs   rd   r¡   re   r   r}   r¢   r£   r~   r   rR   rS   r¤   r¥   r¦   )r   rB   rž   r    r–   r—   r˜   r¨   r©   ÚpositionrŒ   rŽ   Zx_posZy_posZ
center_posr€   r�   r   r   r   Úmouse_click_with_offset_async  sp    	

þÿ
ùÿÿ
ùÿÿz%Element.mouse_click_with_offset_asyncc              	   Ã  sˆ   z|   ¡ I dH j}W n  ty4   t d| ¡ Y dS 0 tjdg|¢| ‘R Ž  t | j t	j
jd|d |d d�¡| j d¡¡I dH  dS )	zƒ
        Moves the mouse to the element position.
        When an element has an hover/mouseover effect, this triggers it.
        NzDid not find location for %sz5Mouse move to location %.2f, %.2f where %s is locatedÚ
mouseMovedr   r   ©rB   rž   gš™™™™™©?)r”   r    rs   rd   re   r¢   Zgatherr   rR   rS   r¤   r¥   Úsleep)r   r    r   r   r   Úmouse_move_async]  s(    ÿþýÿÿ
úzElement.mouse_move_asyncz-typing.Union[Element, typing.Tuple[int, int]]rª   )ÚdestinationÚrelativeÚstepsc              	   ƒ  s  z|   ¡ I dH j‰ W n ty(   Y dS 0 ˆ s>t d| ¡ dS d}t|tƒrŒz|  ¡ I dH j}W n tyt   Y dS 0 |sÄt d|¡ dS n8t|ttfƒrÄ|rÀˆ d |d  ˆ d |d  f}n|}| j	 
tjjdˆ d ˆ d tj d¡d�¡I dH  |�r|dk �r
dn|}|dk�rB| j	 
tjjd|d |d d	�¡I dH  n”|dk�rÖ|d ˆ d  | ‰|d ˆ d  | ‰‡ ‡‡fd
d„t|d ƒD ƒ}|D ]>}| j	 
tjjd|d |d d	�¡I dH  t d¡I dH  �q–| j	 
tjjd|d |d tj d¡d�¡I dH  dS )aµ  
        Drags an element to another element or target coordinates.
        Dragging of elements should be supported by the site.
        :param destination: Another element where to drag to,
            or a tuple (x,y) of ints representing coordinates.
        :type destination: Element or coordinate as x,y tuple
        :param relative: when True, treats coordinate as relative.
        For example (-100, 200) will move left 100px and down 200px.
        :type relative:
        :param steps: Move in <steps> points.
            This could make it look more "natural" (default 1),
            but also a lot slower. (For very smooth actions, use 50-100)
        :type steps: int
        Nrš   r   r   rœ   r‹   )rB   rž   r–   r­   r®   c                   s,   g | ]$}ˆ d  ˆ|  ˆ d ˆ|  f‘qS )r   r   r   )Ú.0Úi©Zstart_pointZstep_size_xZstep_size_yr   r   Ú
<listcomp>·  s   ýþz,Element.mouse_drag_async.<locals>.<listcomp>rŸ   )Útype_rB   rž   r–   )r”   r    rs   rd   r¡   Ú
isinstancer   ÚtupleÚlistr   rR   rS   r¤   r¥   r¦   Úranger¢   r¯   )r   r±   r²   r³   Z	end_pointZpathwayÚpointr   r¶   r   Úmouse_drag_asyncu  s€    
ÿþ
üÿ

ýÿ

ûýÿ

üÿzElement.mouse_drag_asyncc                 Ã  s  � zþ| j  tj ¡ ¡I d H }|d }|j}|j}||j }||j }| j  tj	j
| jd�¡I d H }|j}t|d |d |d |d ƒ}	t|d |d |d |d	 ƒ}
t|d |d |d |d ƒ}t|d |d |d |d	 ƒ}|	|koþ||koþ|
|koþ||kW S  t�y   Y d
S 0 d S )Né   rQ   r   rŠ   é   r   é   é   é   F)r   rR   rS   ÚpageZget_layout_metricsZpage_xZpage_yZclient_widthZclient_heightrT   Zget_box_modelr$   ÚcontentÚminÚmaxr   )r   ZlayoutÚviewportZv_leftZv_topZv_rightZv_bottomÚboxZquadZe_leftZe_topZe_rightZe_bottomr   r   r   Úis_in_viewport_asyncÐ  s0    ÿ


ÿ
ÿþzElement.is_in_viewport_asyncc              
   Ã  sZ   z"| j  tjj| jd�¡I dH  W n2 tyT } zt d|¡ W Y d}~dS d}~0 0 dS )zScrolls element into view.rQ   NzCould not scroll into view: %s)	r   rR   rS   rT   Zscroll_into_view_if_neededr$   r   rd   re   ©r   Úer   r   r   Úscroll_into_view_asyncé  s    ÿÿzElement.scroll_into_view_asyncc              
   Ã  sJ   z|   d¡I dH  W n0 tyD } zt d|¡ W Y d}~n
d}~0 0 dS )zClears an input field.z*function (element) { element.value = "" } Nz!Could not clear element field: %s)rƒ   r   rd   re   rË   r   r   r   Úclear_input_asyncö  s
    "zElement.clear_input_async©Útextc                 ƒ  s0   ˆ   d¡I dH  ‡ fdd„t|ƒD ƒI dH  dS )a  
        Send text to an input field, or any other html element.
        Hint: If you ever get stuck where using py:meth:`~click`
        does not work, sending the keystroke \n or \r\n
        or a spacebar works wonders!
        :param text: text to send
        :return: None
        z(elem) => elem.focus()Nc                 “  s*   g | ]"}ˆ j  tjjd |d�¡I dH ‘qS )ÚcharrÏ   N)r   rR   rS   r¤   Zdispatch_key_event)r´   rÑ   r    r   r   r·     s   ýÿz+Element.send_keys_async.<locals>.<listcomp>)rƒ   r»   )r   rÐ   r   r    r   Úsend_keys_asyncþ  s    	
üzElement.send_keys_asyncr   )Ú
file_pathsc                 Ç  s:   dd„ |D ƒ}| j  tjjg |¢| j| jd�¡I dH  dS )a�  
        Some form input require a file (upload).
        A full path needs to be provided.
        This method sends 1 or more file(s) to the input field.
        Make sure the field accepts multiple files in order to send more files.
        (Otherwise the browser might crash.)
        Example:
        `await fileinputElement.send_file('c:/tmp/img.png', 'c:/dir/lol.gif')`
        c                 S  s   g | ]}t |ƒ‘qS r   )rV   )r´   Úpr   r   r   r·     rZ   z+Element.send_file_async.<locals>.<listcomp>)Úfilesr$   rr   N)r   rR   rS   rT   Zset_file_input_filesr$   rr   )r   rÓ   r   r   r   Úsend_file_async  s    
ýÿzElement.send_file_asyncc                 Ã  s   |   d¡I dH S )z@Focus the current element. Often useful in form (select) fields.z(element) => element.focus()Nr†   r    r   r   r   Úfocus_async"  s    zElement.focus_asyncc                 Ã  s   | j dkr|  d¡I dH  dS )zø
        For form (select) fields. When you have queried the options
        you can call this method on the option object.
        Calling :func:`option.select_option()` uses option as selected value.
        (Does not work in all cases.)
        ZOPTIONzè
                (o) => {
                    o.selected = true;
                    o.dispatchEvent(new Event(
                        'change', {view: window,bubbles: true})
                    )
                }
                N)r   rƒ   r    r   r   r   Úselect_option_async&  s    
ÿzElement.select_option_asyncc                 Ã  s$   | j  tjj| j|d�¡I d H  d S )N©r#   r1   )r   rR   rS   rT   Úset_node_valuer#   )r   r1   r   r   r   Úset_value_async9  s    ÿzElement.set_value_asyncc                 Ã  sz   | j dksH| jdkr@| jd }| |¡I d H  |  ¡ I d H  d S tdƒ‚|  ¡ I d H  | j tj	j
| j|d�¡I d H  d S )NrÁ   r   r   z#Could only set value of text nodes.rÙ   )r%   r)   rm   Úset_text_asyncrW   rk   r   rR   rS   rT   rÚ   r#   )r   r1   Ú
child_noder   r   r   rÜ   >  s    


ÿzElement.set_text_asyncc                 Ã  s    | j  tjj| jdd�¡I d H S )NT)r$   Zinclude_shadow_dom)r   rR   rS   rT   Zget_outer_htmlr$   r    r   r   r   Úget_html_asyncL  s    þÿzElement.get_html_asyncc                 C  s$   t  | jdd„ ¡}|r |j ¡ S dS )z4Gets the text content of this specific element node.c                 S  s
   | j dkS ©NrÁ   ©r%   r`   r   r   r   rY   W  rZ   z'Element.text_fragment.<locals>.<lambda>Ú )r[   r\   r   r'   Ústrip)r   Z	text_noder   r   r   Útext_fragmentT  s    
zElement.text_fragmentc                 C  s’   t tƒ�P | jj ¡ dv rJ| jjd jd jd }|rJ|jW  d  ƒ S W d  ƒ n1 s^0    Y  t 	| jdd„ ¡}d 
dd	„ |D ƒ¡ ¡ S )
z¥
        Gets the text contents of this element and child nodes, concatenated.
        Note: This includes text in the form of script content, (text nodes).
        ©ÚinputZtextarear   r_   Nc                 S  s
   | j dkS rß   rà   r`   r   r   r   rY   h  rZ   zElement.text.<locals>.<lambda>ú c                 S  s   g | ]
}|j ‘qS r   ©r'   ©r´   ra   r   r   r   r·   j  rZ   z Element.text.<locals>.<listcomp>©r   r   r   r   r   r7   rm   r'   r[   Zfilter_recurse_allÚjoinrâ   ©r   Z
input_nodeZ
text_nodesr   r   r   rÐ   \  s    
2
ÿzElement.textc                 C  s’   t tƒ�P | jj ¡ dv rJ| jjd jd jd }|rJ|jW  d  ƒ S W d  ƒ n1 s^0    Y  t 	| jdd„ ¡}d 
dd	„ |D ƒ¡ ¡ S )
z1Same as text(). Kept for backwards compatibility.rä   r   r_   Nc                 S  s
   | j dkS rß   rà   r`   r   r   r   rY   u  rZ   z"Element.text_all.<locals>.<lambda>ræ   c                 S  s   g | ]
}|j ‘qS r   rç   rè   r   r   r   r·   w  rZ   z$Element.text_all.<locals>.<listcomp>ré   rë   r   r   r   Útext_alll  s    
2
ÿzElement.text_all)Úselectorc                 Ã  s$   |   ¡ I dH  | jj|| d�I dH S )zLike JS querySelectorAll()Nrh   )rW   r   Zquery_selector_all©r   rí   r   r   r   Úquery_selector_all_asyncy  s    z Element.query_selector_all_asyncc                 Ã  s"   |   ¡ I dH  | j || ¡I dH S )zLike JS querySelector()N)rW   r   Zquery_selectorrî   r   r   r   Úquery_selector_async~  s    zElement.query_selector_asyncÚautoÚpngztyping.Optional[PathLike]ztyping.Optional[str]z)typing.Optional[typing.Union[int, float]])ÚfilenameÚformatÚscalec                 Ã  s²  ddl }ddl}ddl}|  ¡ I dH }|s2tdƒ‚| |¡}d}	| j ¡ I dH  |r\|dkrö|j 	| jj
j¡}
|
j d¡}|d }| dd¡d }|j ¡  d	¡}|
j› d
|› d|› �}d}| ¡ dv rÒd}d}n| ¡ dv ræd}d}t || ¡}	nD| ¡  d¡�rd}n$| ¡  d¡�s,| ¡  d¡�r0d}t |¡}	|	jjddd� | j tjj||dd�¡I dH }|�s„ddlm} |dƒ‚| |¡}|	�s td| ƒ‚|	  |¡ t!|	ƒS )au  
        Saves a screenshot of this element (only).
        This is not the same as :py:obj:`Tab.save_screenshot`,
        which saves a "regular" screenshot.
        When the element is hidden, or has no size,
        or is otherwise not capturable, a RuntimeError is raised.
        :param filename: uses this as the save path
        :type filename: PathLike
        :param format: jpeg or png (defaults to png)
        :type format: str
        :param scale: the scale of the screenshot,
         eg: 1 = size as is, 2 = double, 0.5 is half.
        :return: the path/filename of saved screenshot
        :rtype: str
        r   NzVCould not determine position of element. Probably because it's not in view, or hidden.rñ   ú/r_   ú?r   z%Y-%m-%d_%H-%M-%SÚ__rE   rá   )ZjpgÚjpegz.jpgrù   )rò   z.pngrò   z.jpegT)ÚparentsÚexist_ok)ZclipZcapture_beyond_viewport©ÚProtocolExceptionzXCould not take screenshot. Most possible cause is the page has not finished loading yet.zInvalid filename or path: '%s')"Úurllib.parseÚdatetimeÚbase64r”   rk   Úto_viewportr   r¯   ÚparseÚurlparseÚtargetÚurlÚpathÚsplitÚrsplitÚnowÚstrftimeÚhostnamer   ÚpathlibÚPathÚendswithrl   Úmkdirr   rR   rS   rÄ   Zcapture_screenshotÚ
connectionrý   Ú	b64decodeÚwrite_bytesrV   )r   ró   rô   rõ   Úurllibrÿ   r   r“   rÈ   r  ÚparsedÚpartsZ	last_partZdt_strÚ	candidateÚextÚdatarý   Z
data_bytesr   r   r   Úsave_screenshot_asyncƒ  sd    ÿ
ÿþ
ÿÿ
ÿ

zElement.save_screenshot_asyncç      à?ÚEE4488)ÚdurationÚcolorr¨   r©   c           
   	   Ã  s2  ddl m} | jsLz$| j tjj| jd�¡I dH | _	W n |yJ   Y dS 0 z|  
¡ I dH }W n  tfy~   t d¡ Y dS 0 d |jd | d |jd | d ||¡}d	 |t d
¡t|d ƒ¡ dd¡ dd¡}z@tjj| j	jd�g}	| j tjj|| j	j|	ddd�¡I dH  W n t�y,   Y n0 dS )z¿
        Displays for a short time a red dot on the element.
        (Only if the element itself is visible)
        :param coords: x,y
        :param duration: seconds (default 0.5)
        r   rü   rQ   Nz'flash() : Could not determine position.zÀposition:absolute;z-index:99999999;padding:0;margin:0;left:{:.1f}px; top: {:.1f}px; opacity:0.7;width:8px;height:8px;border-radius:50%;background:#{};animation:show-pointer-ani {:.2f}s ease 1;r   r¿   a�  
            (targetElement) => {{
                var css = document.styleSheets[0];
                for( let css of [...document.styleSheets]) {{
                    try {{
                        css.insertRule(`
                        @keyframes show-pointer-ani {{
                              0% {{ opacity: 1; transform: scale(2, 2);}}
                              25% {{ transform: scale(5,5) }}
                              50% {{ transform: scale(3, 3);}}
                              75%: {{ transform: scale(2,2) }}
                              100% {{ transform: scale(1, 1); opacity: 0;}}
                        }}`,css.cssRules.length);
                        break;
                    }} catch (e) {{
                        console.log(e)
                    }}
                }};
                var _d = document.createElement('div');
                _d.style = `{0:s}`;
                _d.id = `{1:s}`;
                document.body.insertAdjacentElement('afterBegin', _d);
                setTimeout(
                    () => document.getElementById('{1:s}').remove(), {2:d}
                );
            }}
            é   éè  z  rá   Ú
rt   T)rr   rw   rx   ry   )r  rý   rq   r   rR   rS   rT   rU   r$   r   r”   r   rd   re   rô   r    ÚsecretsZ	token_hexrª   Úreplacer{   r|   rr   r   r   )
r   r  r  r¨   r©   rý   r“   Ústyler€   rw   r   r   r   r~   Ð  sZ    ÿ
ÿ÷
ãá àÿ#ÿûÿ	zElement.flash_asyncc                 Ã  sÎ   t | ddƒr\| `| j tj ¡ ¡I dH  | j tj ¡ ¡I dH  | j tj ¡ ¡I dH  dS | j tj 	¡ ¡I dH  | j tj 	¡ ¡I dH  tjj
dddd�}| j tjj|| jd�¡I dH  t| ddƒ dS )zp
        Highlights the element devtools-style.
        To remove the highlight, call the method again.
        Ú_is_highlightedFNT)Z	show_infoZshow_extension_linesZshow_styles)Zhighlight_configr$   r   )r?   r$  r   rR   rS   ÚoverlayZhide_highlightrT   ÚdisableÚenableZHighlightConfigZhighlight_noder$   Úsetattr)r   Zconfr   r   r   Úhighlight_overlay_async)  s"    ÿÿÿ
zElement.highlight_overlay_async)ró   Úfolderr  c                 Ã  sÈ   | j dkrtdƒ‚|s&tj ¡ d }n
t |¡}|jdd� | j tj	j
dt|ƒd�¡I dH  | d	ƒI dH  d
}|  dj|rˆd|› d�n||ršt|d ƒndd�¡I dH  | dƒI dH  | jI dH  dS )aÚ  
        Experimental option.
        :param filename: the desired filename
        :param folder: the download folder path
        :param duration: record for this many seconds and then download
        On html5 video nodes,
        you can call this method to start recording of the video.
        When any of the follow happens, the video recorded will be downloaded:
            - video ends
            - calling videoelement('pause')
            - video stops
        ZVIDEOz9record_video() can only be called on html5 video elementsZ	downloadsT)rû   Zallow)Údownload_pathNZpausezdocument.title + ".mp4"a  
            function extractVid(vid) {{
                    var duration = {duration:.1f};
                    var stream = vid.captureStream();
                    var mr = new MediaRecorder(
                        stream, {{audio:true, video:true}}
                    )
                    mr.ondataavailable  = function(e) {{
                        vid['_recording'] = false
                        var blob = e.data;
                        f = new File(
                            [blob], {{name: {filename}, type:'octet/stream'}}
                        );
                        var objectUrl = URL.createObjectURL(f);
                        var link = document.createElement('a');
                        link.setAttribute('href', objectUrl)
                        link.setAttribute('download', {filename})
                        link.style.display = 'none'
                        document.body.appendChild(link)
                        link.click()
                        document.body.removeChild(link)
                    }}
                    mr.start()
                    vid.addEventListener('ended' , (e) => mr.stop())
                    vid.addEventListener('pause' , (e) => mr.stop())
                    vid.addEventListener('abort', (e) => mr.stop())
                    if ( duration ) {{
                        setTimeout(
                            () => {{ vid.pause(); vid.play() }}, duration
                        );
                    }}
                    vid['_recording'] = true
            ;}}
            ú"r  r   )ró   r  Zplay)r   rk   r  r  Úcwdr  r   rR   rS   ZbrowserZset_download_behaviorrV   rƒ   rô   rª   )r   ró   r*  r  Zdirectory_pathZdtmr   r   r   Úrecord_video_async@  s.    
ÿ
ÿÿ
"Ýÿ
'zElement.record_video_asyncc                 Ã  s   |   d¡I d H S )Nz(vid) => vid["_recording"]r†   r    r   r   r   Úis_recording_asyncŒ  s    zElement.is_recording_asyncc                 C  sZ   d }| j jrVt| j jƒD ]<\}}|dks4|d dkrF|dkr@d}|}q|r|| j|< qd S )Nr   rŠ   ÚclassÚclass_)r   r*   Ú	enumerater@   )r   Zsavrµ   Úar   r   r   r   �  s    zElement._make_attrs)Úotherri   c                 C  s   |j r| j r|j | j kS dS )NFrQ   )r   r4  r   r   r   Ú__eq__›  s    zElement.__eq__c              	   C  s¸   | j j ¡ }d}| jrb| jdkr:| jrb|t| jd ƒ7 }n(| jdkrb| jrb| jD ]}|t|ƒ7 }qP| j jdkr||| j7 }|S d dd„ | j	 
¡ D ƒ¡}d|› d|› d	|› d
|› d	�	}|S )Nrá   r   r   rÁ   ræ   c                 S  s,   g | ]$\}}|d kr|nd› d|› d�‘qS )r1  r0  z="r,  r   )r´   ÚkÚvr   r   r   r·   ´  s   ÿz$Element.__repr__.<locals>.<listcomp>ú<ú>z</)r   r   r   r)   rm   rV   r%   r'   rê   r@   Úitems)r   r"   rÅ   rp   r@   Úsr   r   r   Ú__repr__¢  s(    



þÿzElement.__repr__)N)N)T)F)r‹   r   r   F)Fr‹   r   r   )Fr   )rñ   rò   r   )r  r  r   r   )NNN)VÚ__name__Ú
__module__Ú__qualname__r   Úpropertyr!   r"   r#   r$   r%   r   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r   rC   rH   rN   rP   rX   r^   rW   r   r   Úsetterr@   rl   rm   rq   rr   r‚   r„   r…   r‡   rƒ   r”   r§   r¬   r°   r¾   rÊ   rÍ   rÎ   rÒ   rÖ   r×   rØ   rÛ   rÜ   rÞ   rã   rÐ   rì   rï   rð   r  r~   r)  r.  r/  r   r5  r<  Ú__classcell__r   r   rL   r   r   .   s"   ÿ


































,


+     û:    ùC  ü[

   üO    ûY   üLr   c                      s2   e Zd ZdZ‡ fdd„Zd	dd„Zdd„ Z‡  ZS )
r‰   z%Helper class for element-positioning.c              	     s�   t ƒ  |¡ |\| _| _| _| _| _| _| _| _d| _d| _| j| _| j| _	| j| j | j| j  | _
| _| j| jd  | j| j
d  f| _d S )Nr   rŠ   )rI   r   r‹   r�   ÚrightÚbottomr�   r�   rB   rž   rŽ   rŒ   r    )r   ZpointsrL   r   r   r   À  s(    
÷ÿ
þzPosition.__init__r   c                 C  s   t jj| j| j| j| j|d�S )N)rB   rž   rŒ   rŽ   rõ   )rS   rÄ   ZViewportrB   rž   rŒ   rŽ   )r   rõ   r   r   r   r  Ø  s    ûzPosition.to_viewportc              	   C  s&   d| j › d| j› d| j› d| j› d�	S )Nz<Position(x=z, y=z,
            width=z	, height=z)>
            )r‹   r�   rŒ   rŽ   r    r   r   r   r<  á  s    ÿÿÿzPosition.__repr__)r   )r=  r>  r?  Ú__doc__r   r  r<  rB  r   r   rL   r   r‰   ½  s   
	r‰   zcdp.dom.NodeId)r   r#   c                 Ã  sP   |   tjj|d�¡I d H }|   tj |j¡¡I d H }|   tj |¡¡I d H }|S )N)r#   )rR   rS   rT   rU   Zrequest_noderr   Zdescribe_node)r   r#   Z
remote_objr   r   r   r   rU   é  s    ÿ

ÿrU   )N)#Ú
__future__r   r¢   Úloggingr  r!  ÚtypingÚ
contextlibr   rá   r   r[   Z_contradictr   Úconfigr   ZmycdprS   Zmycdp.input_Z	mycdp.domZmycdp.overlayZ
mycdp.pageZmycdp.runtimeÚ	getLoggerr=  rd   ÚTYPE_CHECKINGr   r   r   r   rT   ZQuadr‰   rU   r   r   r   r   Ú<module>   s@   
 ý         ,