o
    ѷ6iO                     @  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mZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ dadadd ZG dd dedZG dd deZdS )    )annotationsN)ABCMeta)	b64decodeencodebytes)md5)BytesIO)JavascriptExceptionWebDriverException)By)keys_to_typing)Command)
ShadowRootc                  C  s@   d tdd d } t| ddat| ddad S )N.zgetAttribute.jsutf8zisDisplayed.js)join__name__splitpkgutilget_datadecodegetAttribute_jsisDisplayed_js)_pkg r   i/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py_load_js(   s   r   c                   @  s   e Zd ZdZdS )BaseWebElementzAbstract Base Class for WebElement.

    ABC's will allow custom types to be registered as a WebElement to
    pass type checks.
    N)r   
__module____qualname____doc__r   r   r   r   r   0   s    r   )	metaclassc                   @  s  e Zd ZdZdUddZdd ZedVd	d
ZedVddZedVddZ	dUddZ
dUddZdUddZdWddZdVddZdXddZdYddZdYd d!ZdZd#d$Zed[d&d'ZdYd(d)Zed\d+d,Zed\d-d.ZdVd/d0Zed\d1d2Zed\d3d4ZedVd5d6ZedVd7d8ZedVd9d:Zed]d<d=ZdYd>d?Zed@dA ZedVdBdCZ dDdE Z!dFdG Z"d^dIdJZ#e$j%dHfd_dKdLZ&e$j%dHfd`dNdOZ'dadQdRZ(dSdT Z)dHS )b
WebElementa  Represents a DOM element.

    Generally, all interesting operations that interact with a document will be
    performed through this interface.

    All method calls will do a freshness check to ensure that the element
    reference is still valid.  This essentially determines whether the
    element is still attached to the DOM.  If this test fails, then an
    `StaleElementReferenceException` is thrown, and all future calls to this
    instance will fail.
    returnNonec                 C  s   || _ || _d S N)_parent_id)selfparentid_r   r   r   __init__G   s   
zWebElement.__init__c              	   C  s.   dt | j dt | j d| j d| j d	S )N<r   z (session="z", element="z")>)typer   r   
session_idr'   r(   r   r   r   __repr__K   s   .zWebElement.__repr__strc                 C  s   | j jS r%   )r&   r.   r/   r   r   r   r.   N   s   zWebElement.session_idc                 C     |  tjd S )zThis element's `tagName` property.

        Returns:
            The tag name of the element.

        Example:
            element = driver.find_element(By.ID, "foo")
        value)_executer   GET_ELEMENT_TAG_NAMEr/   r   r   r   tag_nameR      
zWebElement.tag_namec                 C  r2   )zThe text of the element.

        Returns:
            The text of the element.

        Example:
            element = driver.find_element(By.ID, "foo")
            print(element.text)
        r3   )r4   r   GET_ELEMENT_TEXTr/   r   r   r   text^   s   zWebElement.textc                 C     |  tj dS )zClicks the element.

        Example:
            element = driver.find_element(By.ID, "foo")
            element.click()
        N)r4   r   CLICK_ELEMENTr/   r   r   r   clickk      zWebElement.clickc              
   C  s<   d}z
| j ||  W dS  ty } ztd|d}~ww )z}Submits a form.

        Example:
            form = driver.find_element(By.NAME, "login")
            form.submit()
        a  /* submitForm */var form = arguments[0];
while (form.nodeName != "FORM" && form.parentNode) {
  form = form.parentNode;
}
if (!form) { throw Error('Unable to find containing form element'); }
if (!form.ownerDocument) { throw Error('Unable to find owning document'); }
var e = form.ownerDocument.createEvent('Event');
e.initEvent('submit', true, true);
if (form.dispatchEvent(e)) { HTMLFormElement.prototype.submit.call(form) }
z=To submit an element, it must be nested inside a form elementN)r&   execute_scriptr   r	   )r(   scriptexcr   r   r   submitt   s   
zWebElement.submitc                 C  r:   )zClears the text if it's a text entry element.

        Example:
            text_field = driver.find_element(By.NAME, "username")
            text_field.clear()
        N)r4   r   CLEAR_ELEMENTr/   r   r   r   clear   r=   zWebElement.clearstr | bool | WebElement | dictc                 C  s<   z|  tjd|id W S  ty   | jd| | Y S w )a  Gets the given property of the element.

        Args:
            name: Name of the property to retrieve.

        Returns:
            The value of the property.

        Example:
            text_length = target_element.get_property("text_length")
        namer3   z!return arguments[0][arguments[1]])r4   r   GET_ELEMENT_PROPERTYr	   r)   r>   r(   rE   r   r   r   get_property   s
   zWebElement.get_propertyc                 C     |  tjd|id S )a  Get the HTML attribute value (not reflected properties) of the element.

        Returns only attributes declared in the element's HTML markup, unlike
        `selenium.webdriver.remote.BaseWebElement.get_attribute`.

        Args:
            name: Name of the attribute to retrieve.

        Returns:
            The value of the attribute.

        Example:
            text_length = target_element.get_dom_attribute("class")
        rE   r3   )r4   r   GET_ELEMENT_ATTRIBUTErG   r   r   r   get_dom_attribute   s   zWebElement.get_dom_attribute
str | Nonec                 C  s*   t du rt  | jdt  d| |}|S )az  Gets the given attribute or property of the element.

        This method will first try to return the value of a property with the
        given name. If a property with that name doesn't exist, it returns the
        value of the attribute with the same name. If there's no attribute with
        that name, ``None`` is returned.

        Values which are considered truthy, that is equals "true" or "false",
        are returned as booleans.  All other non-``None`` values are returned
        as strings.  For attributes or properties which do not exist, ``None``
        is returned.

        To obtain the exact value of the attribute or property,
        use :func:`~selenium.webdriver.remote.BaseWebElement.get_dom_attribute` or
        :func:`~selenium.webdriver.remote.BaseWebElement.get_property` methods respectively.

        Args:
            name: Name of the attribute/property to retrieve.

        Returns:
            The value of the attribute/property.

        Example:
            # Check if the "active" CSS class is applied to an element.
            is_active = "active" in target_element.get_attribute("class")
        Nz/* getAttribute */return ().apply(null, arguments);)r   r   r)   r>   )r(   rE   attribute_valuer   r   r   get_attribute   s   zWebElement.get_attributeboolc                 C  r2   )zReturns whether the element is selected.

        This method is generally used on checkboxes, options in a select
        and radio buttons.

        Example:
            is_selected = element.is_selected()
        r3   )r4   r   IS_ELEMENT_SELECTEDr/   r   r   r   is_selected   s   	zWebElement.is_selectedc                 C  r2   )zpReturns whether the element is enabled.

        Example:
            is_enabled = element.is_enabled()
        r3   )r4   r   IS_ELEMENT_ENABLEDr/   r   r   r   
is_enabled   s   zWebElement.is_enabledr3   c                   s    j jr2tt fdddtt|d}d|vr2g }|D ]
}| | q t	d|} 
tjdt|t|d dS )a  Simulates typing into the element.

        Use this to send simple key events or to fill out form fields.
        This can also be used to set file inputs.

        Args:
            value: A string for typing, or setting form fields. For setting
                file inputs, this could be a local file path.

        Examples:
            To send a simple key event::

            form_textfield = driver.find_element(By.NAME, "username")
            form_textfield.send_keys("admin")

            or to set a file input field::

            file_input = driver.find_element(By.NAME, "profilePic")
            file_input.send_keys("path/to/profilepic.gif")
            # Generally it's better to wrap the file path in one of the methods
            # in os.path to return the actual path to support cross OS testing.
            # file_input.send_keys(os.path.abspath("path/to/profilepic.gif"))
        c                   s    j jt| S r%   )r)   file_detectoris_local_filer1   )keys_to_sendr/   r   r   <lambda>
  s    z&WebElement.send_keys.<locals>.<lambda> 
N)r9   r3   )r)   
_is_remotelistmapr   r1   r   append_uploadtupler4   r   SEND_KEYS_TO_ELEMENTr   )r(   r3   local_filesremote_filesfiler   r/   r   	send_keys   s   
zWebElement.send_keysr   c                 C  r2   )a  Get the shadow root attached to this element if present (Chromium, Firefox, Safari).

        Returns:
            The ShadowRoot object.

        Raises:
            NoSuchShadowRoot: If no shadow root was attached to element.

        Example:
            try:
                shadow_root = element.shadow_root
            except NoSuchShadowRoot:
                print("No shadow root attached to element")
        r3   )r4   r   GET_SHADOW_ROOTr/   r   r   r   shadow_root  s   zWebElement.shadow_rootc                 C  s$   t du rt  | jdt  d| S )zvWhether the element is visible to a user.

        Example:
            is_displayed = element.is_displayed()
        Nz/* isDisplayed */return (rM   )r   r   r)   r>   r/   r   r   r   is_displayed+  s   zWebElement.is_displayeddictc                 C  s4   |  tjd| gdd }t|d t|d dS )a  Get the element's location on screen after scrolling it into view.

        This may change without warning and scrolls the element into view
        before calculating coordinates for clicking purposes.

        Returns:
            The top lefthand corner location on the screen, or zero
            coordinates if the element is not visible.

        Example:
            loc = element.location_once_scrolled_into_view
        zNarguments[0].scrollIntoView(true); return arguments[0].getBoundingClientRect())r?   argsr3   xyrk   rl   )r4   r   W3C_EXECUTE_SCRIPTround)r(   old_locr   r   r    location_once_scrolled_into_view6  s   z+WebElement.location_once_scrolled_into_viewc                 C  s&   |  tjd }|d |d d}|S )zGet the size of the element.

        Returns:
            The width and height of the element.

        Example:
            size = element.size
        r3   heightwidth)rr   rs   r4   r   GET_ELEMENT_RECT)r(   sizenew_sizer   r   r   rv   M  s   
zWebElement.sizec                 C  rI   )a  Get the value of a CSS property.

        Args:
            property_name: The name of the CSS property to get the value of.

        Returns:
            The value of the CSS property.

        Example:
            value = element.value_of_css_property("color")
        propertyNamer3   )r4   r   !GET_ELEMENT_VALUE_OF_CSS_PROPERTY)r(   property_namer   r   r   value_of_css_property[  s   z WebElement.value_of_css_propertyc                 C  s.   |  tjd }t|d t|d d}|S )zGet the location of the element in the renderable canvas.

        Returns:
            The x and y coordinates of the element.

        Example:
            loc = element.location
        r3   rk   rl   rm   )r4   r   ru   ro   )r(   rp   new_locr   r   r   locationi  s   
zWebElement.locationc                 C  r2   )zGet the size and location of the element.

        Returns:
            A dictionary with size and location of the element.

        Example:
            rect = element.rect
        r3   rt   r/   r   r   r   rectw  r7   zWebElement.rectc                 C  r2   )zGet the ARIA role of the current web element.

        Returns:
            The ARIA role of the element.

        Example:
            role = element.aria_role
        r3   )r4   r   GET_ELEMENT_ARIA_ROLEr/   r   r   r   	aria_role  r7   zWebElement.aria_rolec                 C  r2   )zGet the ARIA Level of the current webelement.

        Returns:
            The ARIA Level of the element.

        Example:
            name = element.accessible_name
        r3   )r4   r   GET_ELEMENT_ARIA_LABELr/   r   r   r   accessible_name  r7   zWebElement.accessible_namec                 C  r2   )zGet a base64-encoded screenshot of the current element.

        Returns:
            The screenshot of the element as a base64 encoded string.

        Example:
            img_b64 = element.screenshot_as_base64
        r3   )r4   r   ELEMENT_SCREENSHOTr/   r   r   r   screenshot_as_base64  r7   zWebElement.screenshot_as_base64bytesc                 C  s   t | jdS )zGet the screenshot of the current element as a binary data.

        Returns:
            The screenshot of the element as binary data.

        Example:
            element_png = element.screenshot_as_png
        ascii)r   r   encoder/   r   r   r   screenshot_as_png  r7   zWebElement.screenshot_as_pngc                 C  s   |  dstdt | j}z1z$t|d}|| W d   n1 s'w   Y  W W ~dS W W ~dS  tyA   Y W ~dS w ~w )a  Save a PNG screenshot of the current element to a file.

        Use full paths in your filename.

        Args:
            filename: The full path you wish to save your screenshot to. This
                should end with a `.png` extension.

        Returns:
            True if the screenshot was saved successfully, False otherwise.

        Example:
            element.screenshot("/Screenshots/foo.png")
        z.pngz^name used for saved screenshot does not match file type. It should end with a `.png` extensionwbNFT)	lowerendswithwarningswarnUserWarningr   openwriteOSError)r(   filenamepngfr   r   r   
screenshot  s(    zWebElement.screenshotc                 C     | j S )zGet the WebDriver instance this element was found from.

        Example:
            element = driver.find_element(By.ID, "foo")
            parent_element = element.parent
        )r&   r/   r   r   r   r)     s   zWebElement.parentc                 C  r   )a"  Get the ID used by selenium.

        This is mainly for internal use. Simple use cases such as checking if 2
        webelements refer to the same element, can be done using ``==``::

        Example:
            if element1 == element2:
                print("These 2 are equal")
        )r'   r/   r   r   r   id  s   zWebElement.idc                 C  s   t |do
| j|jkS )Nr   )hasattrr'   r   r(   elementr   r   r   __eq__  s   zWebElement.__eq__c                 C  s   |  | S r%   )r   r   r   r   r   __ne__  s   zWebElement.__ne__Nc                 C  s    |si }| j |d< | j||S )a?  Executes a command against the underlying HTML element.

        Args:
            command: The name of the command to _execute as a string.
            params: A dictionary of named Parameters to send with the command.

        Returns:
            The command's JSON response loaded into a dictionary object.
        r   )r'   r&   execute)r(   commandparamsr   r   r   r4     s   

zWebElement._executec                 C  ,   | j j||\}}| tj||dd S )a  Find an element given a By strategy and locator.

        Args:
            by: The locating strategy to use. Default is `By.ID`. Supported values include:
                - By.ID: Locate by element ID.
                - By.NAME: Locate by the `name` attribute.
                - By.XPATH: Locate by an XPath expression.
                - By.CSS_SELECTOR: Locate by a CSS selector.
                - By.CLASS_NAME: Locate by the `class` attribute.
                - By.TAG_NAME: Locate by the tag name (e.g., "input", "button").
                - By.LINK_TEXT: Locate a link element by its exact text.
                - By.PARTIAL_LINK_TEXT: Locate a link element by partial text match.
                - RelativeBy: Locate elements relative to a specified root element.
            value: The locator value to use with the specified `by` strategy.

        Returns:
            The first matching `WebElement` found on the page.

        Example:
            element = driver.find_element(By.ID, "foo")
        usingr3   r3   )r&   locator_converterconvertr4   r   FIND_CHILD_ELEMENTr(   byr3   r   r   r   find_element     zWebElement.find_elementlist[WebElement]c                 C  r   )a  Find elements given a By strategy and locator.

        Args:
            by: The locating strategy to use. Default is `By.ID`. Supported values include:
                - By.ID: Locate by element ID.
                - By.NAME: Locate by the `name` attribute.
                - By.XPATH: Locate by an XPath expression.
                - By.CSS_SELECTOR: Locate by a CSS selector.
                - By.CLASS_NAME: Locate by the `class` attribute.
                - By.TAG_NAME: Locate by the tag name (e.g., "input", "button").
                - By.LINK_TEXT: Locate a link element by its exact text.
                - By.PARTIAL_LINK_TEXT: Locate a link element by partial text match.
                - RelativeBy: Locate elements relative to a specified root element.
            value: The locator value to use with the specified `by` strategy.

        Returns:
            List of `WebElements` matching locator strategy found on the page.

        Example:
            element = driver.find_elements(By.ID, "foo")
        r   r3   )r&   r   r   r4   r   FIND_CHILD_ELEMENTSr   r   r   r   find_elements  r   zWebElement.find_elementsintc                 C  s   t t| jd dS )Nutf-8   )r   md5_hashr'   r   	hexdigestr/   r   r   r   __hash__0  s   zWebElement.__hash__c              
   C  s   t  }t|dtj}||tj|d  |  t	|
 }t|ts+|d}z| tjd|id W S  tyn } z+dt|v rM|W  Y d }~S dt|v r[|W  Y d }~S dt|v ri|W  Y d }~S  d }~ww )	Nw   r   rd   r3   zUnrecognized command: POSTzCommand not found: POST z.{"status":405,"value":["GET","HEAD","DELETE"]})r   zipfileZipFileZIP_DEFLATEDr   ospathr   closer   getvalue
isinstancer1   r   r4   r   UPLOAD_FILEr	   )r(   r   fpzippedcontenter   r   r   r_   3  s&   

zWebElement._upload)r#   r$   )r#   r1   )r#   rD   )r#   rL   )r#   rP   )r3   r1   r#   r$   )r#   r   )r#   ri   )r#   r   r%   )r#   r"   )r#   r   )r#   r   )*r   r   r   r    r+   r0   propertyr.   r6   r9   r<   rA   rC   rH   rK   rO   rR   rT   re   rg   rh   rq   rv   r{   r}   r~   r   r   r   r   r   r)   r   r   r   r4   r
   IDr   r   r   r_   r   r   r   r   r"   :   sf    


	

	


"

+



	

r"   ) 
__future__r   r   r   r   r   abcr   base64r   r   hashlibr   r   ior   selenium.common.exceptionsr   r	   selenium.webdriver.common.byr
   selenium.webdriver.common.utilsr   !selenium.webdriver.remote.commandr   $selenium.webdriver.remote.shadowrootr   r   r   r   r   r"   r   r   r   r   <module>   s&   
