a
    >xd!H                     @   s   d Z ddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZ erdd	lmZmZ eeZG d
d deZe	e edddZ dS )zElement handle module.    N)AnyDictListOptionalTYPE_CHECKING)
CDPSession)ExecutionContextJSHandle)ElementHandleErrorNetworkError)
debugError)
merge_dict)FrameFrameManagerc                       s  e Zd ZdZeeeeddd fddZd ddd	Z	e
d
 dddZddddZeeef dddZe
e dddZee eeeef  dddZddddZd=eeddddZeedddZddddZddd d!Zd>eeedd"d#d$Zd?eeedd%d&d'Ze
eeef  dd(d)Ze
e dd*d+Zd@eeedd,d-Zee
d  d.d/d0Z eed  d.d1d2Z!eeeed3d4d5Z"eeeed3d6d7Z#e Z$e!Z%e"Z&e#Z'eed  d8d9d:Z(e(Z)e*dd;d<Z+  Z,S )AElementHandlea  ElementHandle class.

    This class represents an in-page DOM element. ElementHandle can be created
    by the :meth:`pyppeteer.page.Page.querySelector` method.

    ElementHandle prevents DOM element from garbage collection unless the
    handle is disposed. ElementHandles are automatically disposed when their
    origin frame gets navigated.

    ElementHandle isinstance can be used as arguments in
    :meth:`pyppeteer.page.Page.querySelectorEval` and
    :meth:`pyppeteer.page.Page.evaluate` methods.
    r   N)contextclientremoteObjectpageframeManagerreturnc                    s2   t  ||| || _|| _|| _|| _d| _d S )NF)super__init___client_remoteObject_page_frameManagerZ	_disposed)selfr   r   r   r   r   	__class__ P/var/www/html/Ranjet/env/lib/python3.9/site-packages/pyppeteer/element_handle.pyr   (   s    zElementHandle.__init__)r   c                 C   s   | S )zReturn this ElementHandle.r    r   r    r    r!   	asElement2   s    zElementHandle.asElementr   c                    sP   | j dd| jdiI dH }|di }t|dts@dS | j|d S )z|Return the content frame for the element handle.

        Return ``None`` if this handle is not referencing iframe.
        zDOM.describeNodeobjectIdNnodeZframeId)r   sendr   get
isinstancestrr   frame)r   ZnodeInfoZnode_objr    r    r!   contentFrame6   s    zElementHandle.contentFramec                    s$   | j d| I d H }|r t|d S )Nas  
            async element => {
                if (!element.isConnected)
                    return 'Node is detached from document';
                if (element.nodeType !== Node.ELEMENT_NODE)
                    return 'Node is not of type HTMLElement';
                const visibleRatio = await new Promise(resolve => {
                    const observer = new IntersectionObserver(entries => {
                        resolve(entries[0].intersectionRatio);
                        observer.disconnect();
                    });
                    observer.observe(element);
                });
                if (visibleRatio !== 1.0)
                    element.scrollIntoView({
                        block: 'center',
                        inline: 'center',
                        behavior: 'instant',
                    });
                return false;
            })executionContextevaluater
   )r   errorr    r    r!   _scrollIntoViewIfNeededC   s
    
z%ElementHandle._scrollIntoViewIfNeededc           
   
      s   d }z$| j dd| jdiI d H }W n. tyV } ztt| W Y d }~n
d }~0 0 |rf|dsntdg }|dD ]$}| |}t	|dkr||
| q||std|d }d}d}|D ]}	||	d 7 }||	d 7 }q|d	 |d	 d
S )NzDOM.getContentQuadsr$   quads0Node is either not visible or not an HTMLElement   r   xy   r3   r4   )r   r&   r   r'   	Exceptionr   loggerr
   _fromProtocolQuad_computeQuadAreaappend)
r   resulter0   Z_quadZ_qquadr3   r4   pointr    r    r!   _clickablePoint\   s6     
zElementHandle._clickablePointc              
      s\   z$| j dd| jdiI d H }W n2 tyV } ztt| d }W Y d }~n
d }~0 0 |S )NzDOM.getBoxModelr$   )r   r&   r   r'   r   r   r8   )r   r<   r=   r    r    r!   _getBoxModelz   s    
zElementHandle._getBoxModelr>   r   c                 C   sD   |d |d d|d |d d|d |d d|d |d	 dgS )
Nr   r2   r6         r5            r    )r   r>   r    r    r!   r9      s
    zElementHandle._fromProtocolQuadc                    sN   |   I dH  |  I dH }|dd}|dd}| jj||I dH  dS )zMove mouse over to center of this element.

        If needed, this method scrolls element into view. If this element is
        detached from DOM tree, the method raises an ``ElementHandleError``.
        Nr3   r   r4   )r/   r@   r'   r   mousemove)r   objr3   r4   r    r    r!   hover   s
    zElementHandle.hover)optionskwargsr   c                    sZ   t ||}|  I dH  |  I dH }|dd}|dd}| jj|||I dH  dS )a  Click the center of this element.

        If needed, this method scrolls element into view. If the element is
        detached from DOM, the method raises ``ElementHandleError``.

        ``options`` can contain the following fields:

        * ``button`` (str): ``left``, ``right``, of ``middle``, defaults to
          ``left``.
        * ``clickCount`` (int): Defaults to 1.
        * ``delay`` (int|float): Time to wait between ``mousedown`` and
          ``mouseup`` in milliseconds. Defaults to 0.
        Nr3   r   r4   )r   r/   r@   r'   r   rH   click)r   rL   rM   rJ   r3   r4   r    r    r!   rN      s    
zElementHandle.click)	filePathsr   c                    s4   dd |D }| j d}| jd||dI dH S )zUpload files.c                 S   s   g | ]}t j|qS r    )ospathabspath).0pr    r    r!   
<listcomp>       z,ElementHandle.uploadFile.<locals>.<listcomp>r$   zDOM.setFileInputFiles)r$   filesN)r   r'   r   r&   )r   rO   rW   r$   r    r    r!   
uploadFile   s    zElementHandle.uploadFilec                    sN   |   I dH  |  I dH }|dd}|dd}| jj||I dH  dS )zTap the center of this element.

        If needed, this method scrolls element into view. If the element is
        detached from DOM, the method raises ``ElementHandleError``.
        Nr3   r   r4   )r/   r@   r'   r   Ztouchscreentap)r   centerr3   r4   r    r    r!   rY      s
    zElementHandle.tapc                    s   | j d| I dH  dS )zFocus on this element.zelement => element.focus()Nr,   r-   r"   r    r    r!   focus   s    zElementHandle.focus)textrL   rM   r   c                    s2   t ||}|  I dH  | jj||I dH  dS )zqFocus the element and then type text.

        Details see :meth:`pyppeteer.input.Keyboard.type` method.
        N)r   r\   r   keyboardtype)r   r]   rL   rM   r    r    r!   r_      s    
zElementHandle.type)keyrL   rM   r   c                    s2   t ||}|  I dH  | jj||I dH  dS )a  Press ``key`` onto the element.

        This method focuses the element, and then uses
        :meth:`pyppeteer.input.keyboard.down` and
        :meth:`pyppeteer.input.keyboard.up`.

        :arg str key: Name of key to press, such as ``ArrowLeft``.

        This method accepts the following options:

        * ``text`` (str): If specified, generates an input event with this
          text.
        * ``delay`` (int|float): Time to wait between ``keydown`` and
          ``keyup``. Defaults to 0.
        N)r   r\   r   r^   press)r   r`   rL   rM   r    r    r!   ra      s    
zElementHandle.pressc                    s   |   I dH }|sdS |d d }t|d |d |d |d }t|d |d	 |d
 |d }t|d |d |d |d | }t|d |d	 |d
 |d | }||||dS )a  Return bounding box of this element.

        If the element is not visible, return ``None``.

        This method returns dictionary of bounding box, which contains:

        * ``x`` (int): The X coordinate of the element in pixels.
        * ``y`` (int): The Y coordinate of the element in pixels.
        * ``width`` (int): The width of the element in pixels.
        * ``height`` (int): The height of the element in pixels.
        Nmodelborderr   rC   r5   rF   r2   rD   rE   rG   )r3   r4   widthheight)rA   minmax)r   r<   r>   r3   r4   rd   re   r    r    r!   boundingBox   s    ""zElementHandle.boundingBoxc                    sp   |   I dH }|sdS |di }| |d| |d| |d| |d|d|dd	S )
a8  Return boxes of element.

        Return ``None`` if element is not visible. Boxes are represented as an
        list of points; each Point is a dictionary ``{x, y}``. Box points are
        sorted clock-wise.

        Returned value is a dictionary with the following fields:

        * ``content`` (List[Dict]): Content box.
        * ``padding`` (List[Dict]): Padding box.
        * ``border`` (List[Dict]): Border box.
        * ``margin`` (List[Dict]): Margin box.
        * ``width`` (int): Element's width.
        * ``height`` (int): Element's height.
        Nrb   contentpaddingrc   marginrd   re   )ri   rj   rc   rk   rd   re   )rA   r'   r9   )r   r<   rb   r    r    r!   boxModel   s    zElementHandle.boxModelc                    sr  t ||}d}|  I dH }|s(tdt| jj}|d |d ksV|d |d krt|d t	|d t|d t	|d d}t|}|
| | j|I dH  d}|  I dH  |  I dH }|std| jdI dH }|d	 d
 }	|d	 d }
i }|
| |d |	 |d< |d |
 |d< d|i}|
| | j|I dH }|rn| j|I dH  |S )zTake a screenshot of this element.

        If the element is detached from DOM, this method raises an
        ``ElementHandleError``.

        Available options are same as :meth:`pyppeteer.page.Page.screenshot`.
        FNr1   rd   re   )rd   re   TzPage.getLayoutMetricsZlayoutViewportpageXpageYr3   r4   clip)r   rh   r
   copydeepcopyr   Zviewportrg   mathceilupdateZsetViewportr/   r   r&   
screenshot)r   rL   rM   ZneedsViewportResetrh   Zoriginal_viewportZnewViewportZnew_viewport_objrm   rn   ro   optZ	imageDatar    r    r!   ru     sV    





zElementHandle.screenshot)selectorr   c                    s8   | j d| |I dH }| }|r&|S | I dH  dS )zReturn first element which matches ``selector`` under this element.

        If no element matches the ``selector``, returns ``None``.
        z6(element, selector) => element.querySelector(selector)N)r,   evaluateHandler#   dispose)r   rx   handleelementr    r    r!   querySelectorX  s    
zElementHandle.querySelectorc                    s^   | j d| |I dH }| I dH }| I dH  g }| D ]}| }|r>|| q>|S )zReturn all elements which match ``selector`` under this element.

        If no element matches the ``selector``, returns empty list (``[]``).
        z9(element, selector) => element.querySelectorAll(selector)Nr,   ry   ZgetPropertiesrz   valuesr#   r;   )r   rx   arrayHandle
propertiesr<   propelementHandler    r    r!   querySelectorAllg  s    
zElementHandle.querySelectorAll)rx   pageFunctionargsr   c                    sR   |  |I dH }|s$td| d| jj||g|R  I dH }| I dH  |S )a  Run ``Page.querySelectorEval`` within the element.

        This method runs ``document.querySelector`` within the element and
        passes it as the first argument to ``pageFunction``. If there is no
        element matching ``selector``, the method raises
        ``ElementHandleError``.

        If ``pageFunction`` returns a promise, then wait for the promise to
        resolve and return its value.

        ``ElementHandle.Jeval`` is a shortcut of this method.

        Example:

        .. code:: python

            tweetHandle = await page.querySelector('.tweet')
            assert (await tweetHandle.querySelectorEval('.like', 'node => node.innerText')) == 100
            assert (await tweetHandle.Jeval('.retweets', 'node => node.innerText')) == 10
        Nz1Error: failed to find element matching selector "")r}   r
   r,   r-   rz   )r   rx   r   r   r   r<   r    r    r!   querySelectorEvaly  s    
zElementHandle.querySelectorEvalc                    sD   | j d| |I dH }| j j||g|R  I dH }| I dH  |S )a+  Run ``Page.querySelectorAllEval`` within the element.

        This method runs ``Array.from(document.querySelectorAll)`` within the
        element and passes it as the first argument to ``pageFunction``. If
        there is no element matching ``selector``, the method raises
        ``ElementHandleError``.

        If ``pageFunction`` returns a promise, then wait for the promise to
        resolve and return its value.

        Example:

        .. code:: html

            <div class="feed">
                <div class="tweet">Hello!</div>
                <div class="tweet">Hi!</div>
            </div>

        .. code:: python

            feedHandle = await page.J('.feed')
            assert (await feedHandle.JJeval('.tweet', '(nodes => nodes.map(n => n.innerText))')) == ['Hello!', 'Hi!']
        zE(element, selector) => Array.from(element.querySelectorAll(selector))N)r,   ry   r-   rz   )r   rx   r   r   r   r<   r    r    r!   querySelectorAllEval  s    
z"ElementHandle.querySelectorAllEval)
expressionr   c                    s^   | j d| |I dH }| I dH }| I dH  g }| D ]}| }|r>|| q>|S )zEvaluate the XPath expression relative to this elementHandle.

        If there are no such elements, return an empty list.

        :arg str expression: XPath string to be evaluated.
        a  (element, expression) => {
                const document = element.ownerDocument || element;
                const iterator = document.evaluate(expression, element, null,
                    XPathResult.ORDERED_NODE_ITERATOR_TYPE);
                const array = [];
                let item;
                while ((item = iterator.iterateNext()))
                    array.push(item);
                return array;

            }Nr~   )r   r   r   r   r<   propertyr   r    r    r!   xpath  s    

zElementHandle.xpathc                    s   | j d| I dH S )z:Return ``True`` if the element is visible in the viewport.a}  async element => {
            const visibleRatio = await new Promise(resolve => {
                const observer = new IntersectionObserver(entries => {
                    resolve(entries[0].intersectionRatio);
                    observer.disconnect();
                });
                observer.observe(element);
            });
            return visibleRatio > 0;
        }Nr[   r"   r    r    r!   isIntersectingViewport  s    	z$ElementHandle.isIntersectingViewport)N)N)N)N)-__name__
__module____qualname____doc__r   r   dictr   r   r#   r   r+   r/   r   r)   floatr@   rA   r   intr9   rK   rN   rX   rY   r\   r_   ra   rh   rl   bytesru   r}   r   r   r   JZJJZJevalZJJevalr   ZJxboolr   __classcell__r    r    r   r!   r      sP   
 	
: $r   rB   c                 C   s^   d}t | D ]L\}}| | }| |d t|   }||d |d  |d |d   d 7 }q|S )Nr   r2   r3   r4   rC   )	enumeratelen)r>   Zareai_p1p2r    r    r!   r:     s    *r:   )!r   rp   loggingrr   os.pathrP   typingr   r   r   r   r   Zpyppeteer.connectionr   Zpyppeteer.execution_contextr   r	   Zpyppeteer.errorsr
   r   Zpyppeteer.helperr   Zpyppeteer.utilr   Zpyppeteer.frame_managerr   r   	getLoggerr   r8   r   r   r:   r    r    r    r!   <module>   s$   
   \