a
    5ñ»j W  ã                ²   @   sÊ  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mZ ddlmZ dd	lmZ dd
lmZ ddlmZ e  e¡Zejdejdejdejdejdejdej dej!dej"dej#dej$dej%dej&dej'dej(dej)dej*dej+dej,dej-dej.d ej/d!ej0d"ej1d#ej2d$ej3d%ej4d&ej5d'ej6d(ej7d)ej8d*ej9d+ej:d,ej;d-ej<d.ej=d/ej>d0ej?d1ej@d2ejAd3ejBd4ejCd5ejDd6ejEd7ejFd8ejGd9ejHd:ejId;ejJd<ejKd=ejLd>ejMd?ejNd@ejOdAejPdBejQdCejRdDejSdEejTdFejUdGejVdHejWdIejXdJejYdKejZdLej[dMej\dNej]dOej^dPej_dQej`dRejadSejbdTejcdUejddVejedWejfdXejgdYejhdZejid[ejjd\ejkd]ejld^ejmd_ejnd`ejodaejpdbejqdcejrddiYZsG dedf„ dfƒZtdS )gé    N)Ú	b64encode)ÚOptional)Úparse)Úurlparse)Ú__version__é   )Úutils)ÚClientConfig)ÚCommand)Ú	ErrorCode)ÚPOSTz/session)ÚDELETEz/session/$sessionId)ÚGETú/session/$sessionId/window)r   z"/session/$sessionId/window/handles)r   ú/session/$sessionId/url)r   z/session/$sessionId/forward)r   z/session/$sessionId/back)r   z/session/$sessionId/refresh)r   z /session/$sessionId/execute/sync)r   z!/session/$sessionId/execute/async)r   r   )r   z/session/$sessionId/title)r   z/session/$sessionId/source)r   z/session/$sessionId/screenshot)r   z*/session/$sessionId/element/$id/screenshot)r   z/session/$sessionId/element)r   z/session/$sessionId/elements)r   z"/session/$sessionId/element/active)r   z'/session/$sessionId/element/$id/element)r   z(/session/$sessionId/element/$id/elements)r   z%/session/$sessionId/element/$id/click)r   z%/session/$sessionId/element/$id/clear)r   z$/session/$sessionId/element/$id/text)r   z%/session/$sessionId/element/$id/value)r   z$/session/$sessionId/element/$id/name)r   z(/session/$sessionId/element/$id/selected)r   z'/session/$sessionId/element/$id/enabled)r   z$/session/$sessionId/element/$id/rect)r   z//session/$sessionId/element/$id/attribute/$name)r   z./session/$sessionId/element/$id/property/$name)r   z,/session/$sessionId/element/$id/computedrole)r   z-/session/$sessionId/element/$id/computedlabel)r   z&/session/$sessionId/element/$id/shadow)r   z,/session/$sessionId/shadow/$shadowId/element)r   z-/session/$sessionId/shadow/$shadowId/elements)r   ú/session/$sessionId/cookie)r   r   )r   ú /session/$sessionId/cookie/$name)r   r   )r   r   )r   z/session/$sessionId/frame)r   z /session/$sessionId/frame/parent)r   r   )r   z/session/$sessionId/window/new)r   r   )r   z1/session/$sessionId/element/$id/css/$propertyName)r   z!/session/$sessionId/execute_async)r   ú/session/$sessionId/timeouts)r   r   )r   z!/session/$sessionId/alert/dismiss)r   z /session/$sessionId/alert/accept)r   ú/session/$sessionId/alert/text)r   r   )r   ú/session/$sessionId/actions)r   r   )r   ú/session/$sessionId/window/rect)r   r   )r   z#/session/$sessionId/window/maximize)r   ú/session/$sessionId/orientation)r   r   )r   ú&/session/$sessionId/network_connection)r   r   )r   z/session/$sessionId/se/log)r   z /session/$sessionId/se/log/types)r   ú/session/$sessionId/context)r   z/session/$sessionId/contexts)r   r   )r   z%/session/$sessionId/window/fullscreen)r   z#/session/$sessionId/window/minimize)r   z/session/$sessionId/print)r   z*/session/$sessionId/webauthn/authenticator)r   z;/session/$sessionId/webauthn/authenticator/$authenticatorId)r   zF/session/$sessionId/webauthn/authenticator/$authenticatorId/credential)r   úG/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials)r   zU/session/$sessionId/webauthn/authenticator/$authenticatorId/credentials/$credentialId)r   r   )r   z>/session/$sessionId/webauthn/authenticator/$authenticatorId/uv)r   z/session/$sessionId/se/file)r   ú/session/$sessionId/se/files)r   r   )r   r   )r   z"/session/$sessionId/fedcm/gettitle)r   z'/session/$sessionId/fedcm/getdialogtype)r   z%/session/$sessionId/fedcm/accountlist)r   z+/session/$sessionId/fedcm/clickdialogbutton)r   z&/session/$sessionId/fedcm/canceldialog)r   z'/session/$sessionId/fedcm/selectaccount)r   z)/session/$sessionId/fedcm/setdelayenabled)r   z'/session/$sessionId/fedcm/resetcooldownc                   @   sj  e Zd ZU dZdZddlZddlZddlZe ¡ Z	dej
v rFe d¡ne ¡ ZdZeed< e ¡  ¡ ZedkrrdZdZde› d	e› 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d2dd„ƒZdd„ Z dd„ Z!dd„ Z"d3e#e$ e#e% e#e% e#e% e#e& e#e d!œd"d#„Z'i Z(d$d%„ Z)e$d&œd'd(„Z*d)d*„ Z+d4d+d,„Z,d-d.„ Z-d5d0d1„Z.dS )6ÚRemoteConnectionz¹A connection with the Remote WebDriver server.

    Communicates with the server using the WebDriver wire protocol:
    https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
    Nr   ÚREQUESTS_CA_BUNDLEÚ_client_configÚdarwinÚmacz	selenium/z	 (python ú)c                 C   s   | j S )N)r   ©Úself© r$   úQE:\soccer\ml_env\Lib\site-packages\selenium/webdriver/remote/remote_connection.pyÚclient_config§   s    zRemoteConnection.client_configc                 C   s   t jdtdd� | jjS )zp:Returns:

        Timeout value in seconds for all http requests made to the
        Remote Connection
        zWget_timeout() in RemoteConnection is deprecated, get timeout from client_config insteadé   ©Ú
stacklevel©ÚwarningsÚwarnÚDeprecationWarningr   Útimeout©Úclsr$   r$   r%   Úget_timeout«   s    ýzRemoteConnection.get_timeoutc                 C   s   t jdtdd� || j_dS )zyOverride the default timeout.

        :Args:
            - timeout - timeout value for http requests in seconds
        zUset_timeout() in RemoteConnection is deprecated, set timeout in client_config insteadr'   r(   Nr*   )r0   r.   r$   r$   r%   Úset_timeout¹   s    ýzRemoteConnection.set_timeoutc                 C   s   t jdtdd� | j ¡  dS )zAReset the http request timeout to socket._GLOBAL_DEFAULT_TIMEOUT.z_reset_timeout() in RemoteConnection is deprecated, use reset_timeout() in client_config insteadr'   r(   N)r+   r,   r-   r   Úreset_timeoutr/   r$   r$   r%   r3   Ç   s    ýzRemoteConnection.reset_timeoutc                 C   s   t jdtdd� | jjS )zÂ:Returns:

        Paths of the .pem encoded certificate to verify connection to
        command executor. Defaults to certifi.where() or
        REQUESTS_CA_BUNDLE env variable if set.
        zhget_certificate_bundle_path() in RemoteConnection is deprecated, get ca_certs from client_config insteadr'   r(   ©r+   r,   r-   r   Úca_certsr/   r$   r$   r%   Úget_certificate_bundle_pathÑ   s    ýz,RemoteConnection.get_certificate_bundle_pathc                 C   s   t jdtdd� || j_dS )zóSet the path to the certificate bundle to verify connection to
        command executor. Can also be set to None to disable certificate
        validation.

        :Args:
            - path - path of a .pem encoded certificate chain.
        zfset_certificate_bundle_path() in RemoteConnection is deprecated, set ca_certs in client_config insteadr'   r(   Nr4   )r0   Úpathr$   r$   r%   Úset_certificate_bundle_pathà   s    	ýz,RemoteConnection.set_certificate_bundle_pathFc                 C   s|   dd| j dœ}|jrTtjddd� t|j› d|j› � ¡ ƒ}| dd	| ¡ › �i¡ |rf| d
di¡ | j	rx| | j	¡ |S )z²Get headers for remote request.

        :Args:
         - parsed_url - The parsed url
         - keep_alive (Boolean) - Is this a keep-alive connection (default: False)
        zapplication/jsonzapplication/json;charset=UTF-8)ÚAcceptúContent-Typez
User-AgentzREmbedding username and password in URL could be insecure, use ClientConfig insteadr'   r(   ú:ÚAuthorizationzBasic Ú
Connectionz
keep-alive)
Ú
user_agentÚusernamer+   r,   r   ÚpasswordÚencodeÚupdateÚdecodeÚextra_headers)r0   Ú
parsed_urlÚ
keep_aliveÚheadersZbase64stringr$   r$   r%   Úget_remote_connection_headersð   s    
ýÿz.RemoteConnection.get_remote_connection_headersc                 C   s   t | jƒ}|jr|jrdS d S )NT)r   Ú
_proxy_urlr?   r@   )r#   rE   r$   r$   r%   Ú_identify_http_proxy_auth  s    
z*RemoteConnection._identify_http_proxy_authc                 C   s>   t | jƒ}|j› d|j› d|j› �}|j› d|j› �}||fS )Nz://r;   )r   rI   ÚschemeÚhostnameÚportr?   r@   )r#   rE   Zproxy_without_authÚauthr$   r$   r%   Ú_separate_http_proxy_auth  s    
z*RemoteConnection._separate_http_proxy_authc                 C   sà   d| j ji}| | j j di ¡¡ | j jrBd|d< t tjj	¡ n| j j
r^d|d< | j j
|d< | jrÐ| j ¡  d¡r’dd	lm} || jfi |¤ŽS |  ¡ r¼|  ¡ \| _| _tj| jd
�|d< tj| jfi |¤ŽS tjf i |¤ŽS )Nr.   Úinit_args_for_pool_managerÚ	CERT_NONEÚ	cert_reqsÚCERT_REQUIREDr5   Úsockr   )ÚSOCKSProxyManager)Úproxy_basic_authÚproxy_headers)r   r.   rB   rP   ÚgetÚignore_certificatesÚurllib3Údisable_warningsÚ
exceptionsÚInsecureRequestWarningr5   rI   ÚlowerÚ
startswithZurllib3.contrib.socksrU   rJ   rO   Z_basic_proxy_authÚmake_headersÚProxyManagerÚPoolManager)r#   Zpool_manager_init_argsrU   r$   r$   r%   Ú_get_connection_manager  s&    ÿz(RemoteConnection._get_connection_managerT)Úremote_server_addrrF   Úignore_proxyrY   rP   r&   c                 C   sæ   |pt ||||d�| _| jjt_| jjt_| jt_| jjp>tjt_| jjpNtjt_|rft	j
dtdd� |szt	j
dtdd� |rŽt	j
dtdd� |r¢t	j
dtdd� |r¾t	j
dtdd� d | _n| j ¡ | _| jjrÜ|  ¡ | _t| _d S )	N)rd   rF   rY   rP   z\setting remote_server_addr in RemoteConnection() is deprecated, set in client_config insteadr'   r(   zTsetting keep_alive in RemoteConnection() is deprecated, set in client_config insteadz]setting ignore_certificates in RemoteConnection() is deprecated, set in client_config insteadzdsetting init_args_for_pool_manager in RemoteConnection() is deprecated, set in client_config insteadzVsetting ignore_proxy in RemoteConnection() is deprecated, set in client_config instead)r	   r   r.   r   Ú_timeoutr5   Ú	_ca_certsrD   r>   r+   r,   r-   rI   Zget_proxy_urlrF   rc   Ú_connÚremote_commandsÚ	_commands)r#   rd   rF   re   rY   rP   r&   r$   r$   r%   Ú__init__2  s\    	ü

ýýýýý
zRemoteConnection.__init__c                 C   s   ||f| j |< dS )zRegister a new command.N)rj   )r#   ÚnameÚmethodÚurlr$   r$   r%   Úadd_commandu  s    zRemoteConnection.add_command)rl   c                 C   s   | j  |¡S )z Retrieve a command if it exists.)rj   rX   )r#   rl   r$   r$   r%   Úget_commandy  s    zRemoteConnection.get_commandc                 C   sÈ   | j  |¡p| j |¡}|dus.J d|› �ƒ‚|d }t |¡ |¡}dd„ | d¡D ƒ}t|tƒrx|rx|D ]
}||= qlt	 
|¡}| jj› |› �}	|  |¡}
t d|d |	t|
ƒ¡ | j|d |	|d	�S )
am  Send a command to the remote server.

        Any path substitutions required for the URL mapped to the command should be
        included in the command parameters.

        :Args:
         - command - A string specifying the command to execute.
         - params - A dictionary of named parameters to send with the command as
           its JSON payload.
        NzUnrecognised command r   c                 S   s"   h | ]}|  d ¡r|dd… ’qS )ú$r   N©r_   )Ú.0Úwordr$   r$   r%   Ú	<setcomp>Œ  ó    z+RemoteConnection.execute.<locals>.<setcomp>ú/z%s %s %sr   )Úbody)rj   rX   Úextra_commandsÚstringÚTemplateÚ
substituteÚsplitÚ
isinstanceÚdictr   Z	dump_jsonr   rd   Ú_trim_large_entriesÚLOGGERÚdebugÚstrÚ_request)r#   ÚcommandÚparamsÚcommand_infoZpath_stringr7   Zsubstitute_paramsrt   Údatarn   Ztrimmedr$   r$   r%   Úexecute}  s    

zRemoteConnection.executec              	   C   s¦  t  |¡}|  || jj¡}| j ¡ }|r2| |¡ |rB|dvrBd}| jjrl| jj||||| jj	d�}|j
}nJ|  ¡ }	|	�(}
|
j||||| jj	d�}W d  ƒ n1 s¦0    Y  |j
}|j d¡}t d|j
||j¡ �z´d|  krðdk �rn n*|  d|j d	d¡¡W t d
¡ | ¡  S d|  k �r6dk�r’n nX|dk�rb|ddœW t d
¡ | ¡  S ||�srt|ƒn| ¡ dœW t d
¡ | ¡  S g }|j dd¡�rº|j dd¡ d¡}tdd„ |D ƒƒ�slzt | ¡ ¡}W n` t�y@   d|  k �rdk �rn ntj}ntj}|| ¡ dœ Y W t d
¡ | ¡  S 0 d|v�rTd|d< |W t d
¡ | ¡  S d|dœ}|W t d
¡ | ¡  S t d
¡ | ¡  0 dS )aq  Send an HTTP request to the remote server.

        :Args:
         - method - A string for the HTTP method to send the request with.
         - url - A string for the URL to send the request to.
         - body - A string for request body. Ignored unless method is POST or PUT.

        :Returns:
          A dictionary with the server's parsed JSON response.
        )r   ÚPUTN)rx   rG   r.   zUTF-8z1Remote response: status=%s | data=%s | headers=%si,  i0  r   ÚlocationzFinished Requesti�  iô  i‘  zAuthorization Required)ÚstatusÚvaluer:   ú;c                 S   s   g | ]}|  d ¡‘qS )z	image/pngrr   )rs   Úxr$   r$   r%   Ú
<listcomp>¿  rv   z-RemoteConnection._request.<locals>.<listcomp>éÇ   r�   r   )r   r   rH   r   rF   Zget_auth_headerrB   rh   Úrequestr.   rŒ   rc   rˆ   rC   r�   r‚   rG   r„   rX   Úcloserƒ   Ústripr}   Úanyr   Z	load_jsonÚ
ValueErrorr   ÚSUCCESSÚUNKNOWN_ERROR)r#   rm   rn   rx   rE   rG   Úauth_headerÚresponseZ
statuscodeÚconnÚhttprˆ   Úcontent_typerŒ   r$   r$   r%   r„   –  sz    


6
å


è
é

õ

û

ý  ÿ
zRemoteConnection._requestc                 C   s   t | dƒr| j ¡  dS )z<Clean up resources when finished with the remote_connection.rh   N)Úhasattrrh   Úclearr"   r$   r$   r%   r“   Ô  s    
zRemoteConnection.closeéd   c                 C   sj   i }|  ¡ D ]X\}}t|tƒr0|  ||¡||< qt|tƒr\t|ƒ|kr\|d|… d ||< q|||< q|S )a  Truncate string values in a dictionary if they exceed max_length.

        :param dict: Dictionary with potentially large values
        :param max_length: Maximum allowed length of string values
        :return: Dictionary with truncated string values
        Nz...)Úitemsr~   r   r€   rƒ   Úlen)r#   Z
input_dictÚ
max_lengthZoutput_dictionaryÚkeyr�   r$   r$   r%   r€   Ù  s    

z$RemoteConnection._trim_large_entries)F)NTFFNN)N)r    )/Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zbrowser_nameÚosÚsocketÚcertifiÚgetdefaulttimeoutrf   ÚenvironÚgetenvÚwhererg   r   r	   Ú__annotations__ÚplatformÚsystemr^   rD   r   r>   Úpropertyr&   Úclassmethodr1   r2   r3   r6   r8   rH   rJ   rO   rc   r   rƒ   Úboolr   rk   ry   ro   rp   r‰   r„   r“   r€   r$   r$   r$   r%   r   �   sf   




	

      ùùA
>r   )uÚloggingr±   rz   r+   Úbase64r   Útypingr   Úurllibr   Úurllib.parser   rZ   Zseleniumr   Ú r   r&   r	   r…   r
   Zerrorhandlerr   Ú	getLoggerr¥   r�   ZNEW_SESSIONZQUITZW3C_GET_CURRENT_WINDOW_HANDLEZW3C_GET_WINDOW_HANDLESr   Z
GO_FORWARDZGO_BACKZREFRESHZW3C_EXECUTE_SCRIPTZW3C_EXECUTE_SCRIPT_ASYNCZGET_CURRENT_URLZ	GET_TITLEZGET_PAGE_SOURCEZ
SCREENSHOTZELEMENT_SCREENSHOTZFIND_ELEMENTZFIND_ELEMENTSZW3C_GET_ACTIVE_ELEMENTZFIND_CHILD_ELEMENTZFIND_CHILD_ELEMENTSZCLICK_ELEMENTZCLEAR_ELEMENTZGET_ELEMENT_TEXTZSEND_KEYS_TO_ELEMENTZGET_ELEMENT_TAG_NAMEZIS_ELEMENT_SELECTEDZIS_ELEMENT_ENABLEDZGET_ELEMENT_RECTZGET_ELEMENT_ATTRIBUTEZGET_ELEMENT_PROPERTYZGET_ELEMENT_ARIA_ROLEZGET_ELEMENT_ARIA_LABELZGET_SHADOW_ROOTZFIND_ELEMENT_FROM_SHADOW_ROOTZFIND_ELEMENTS_FROM_SHADOW_ROOTZGET_ALL_COOKIESZ
ADD_COOKIEZ
GET_COOKIEZDELETE_ALL_COOKIESZDELETE_COOKIEZSWITCH_TO_FRAMEZSWITCH_TO_PARENT_FRAMEZSWITCH_TO_WINDOWZ
NEW_WINDOWZCLOSEZ!GET_ELEMENT_VALUE_OF_CSS_PROPERTYZEXECUTE_ASYNC_SCRIPTZSET_TIMEOUTSZGET_TIMEOUTSZW3C_DISMISS_ALERTZW3C_ACCEPT_ALERTZW3C_SET_ALERT_VALUEZW3C_GET_ALERT_TEXTZW3C_ACTIONSZW3C_CLEAR_ACTIONSZSET_WINDOW_RECTZGET_WINDOW_RECTZW3C_MAXIMIZE_WINDOWZSET_SCREEN_ORIENTATIONZGET_SCREEN_ORIENTATIONZGET_NETWORK_CONNECTIONZSET_NETWORK_CONNECTIONZGET_LOGZGET_AVAILABLE_LOG_TYPESZCURRENT_CONTEXT_HANDLEZCONTEXT_HANDLESZSWITCH_TO_CONTEXTZFULLSCREEN_WINDOWZMINIMIZE_WINDOWZ
PRINT_PAGEZADD_VIRTUAL_AUTHENTICATORZREMOVE_VIRTUAL_AUTHENTICATORZADD_CREDENTIALZGET_CREDENTIALSZREMOVE_CREDENTIALZREMOVE_ALL_CREDENTIALSZSET_USER_VERIFIEDZUPLOAD_FILEZGET_DOWNLOADABLE_FILESZDOWNLOAD_FILEZDELETE_DOWNLOADABLE_FILESZGET_FEDCM_TITLEZGET_FEDCM_DIALOG_TYPEZGET_FEDCM_ACCOUNT_LISTZCLICK_FEDCM_DIALOG_BUTTONZCANCEL_FEDCM_DIALOGZSELECT_FEDCM_ACCOUNTZSET_FEDCM_DELAYZRESET_FEDCM_COOLDOWNri   r   r$   r$   r$   r%   Ú<module>   sÒ   
�g