o
    衡iX                     @   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	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 d d
lmZ e eZi 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i 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,i ej=d-ej>d.ej?d/ej@d0ejAd1ejBd2ejCd3ejDd4ejEd5ejFd6ejGd7ejHd8ejId9ejJd:ejKd;ejLd<ejMd=i ejNd>ejOd?ejPd@ejQdAejRdBejSdCejTdDejUdEejVdFejWdGejXdHejYdIejZdJej[dKej\dLej]dMej^dNi ej_dOej`dPejadQejbdRejcdSejddTejedUejfdVejgdWejhdXejidYejjdZejkd[ejld\ejmd]ejnd^ejod_ejpd`ejqdaejrdbejsdcejtddiZuG dedf dfZvdS )g    N)	b64encode)parse)unquoteurlparse)__version__)WebDriverException)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/se/event)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                   @   s  e Zd ZU dZdZedB ed< ddlZddlZddl	Z	e
 Zdejv r)edne	 Zeed< ejZedkr:d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d:ddZdd Z dd Z!dd  Z"		!				d;d"edB d#e#d$e#d%e#dB d&e$dB d'edB fd(d)Z%i Z&e$eef ed*< d+d, Z'd-efd.d/Z(d0d1 Z)d<d2e$fd3d4Z*d5d6 Z+d=d2e$eB fd8d9Z,dS )>RemoteConnectionzA connection with the Remote WebDriver server.

    Communicates with the server using the WebDriver wire protocol:
    https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
    Nbrowser_namer   REQUESTS_CA_BUNDLE_client_configdarwinmacz	selenium/z	 (python )c                 C   s   | j S N)r   self r&   |/var/www/html/asbeauty/laura_geller_scraping/env/lib/python3.10/site-packages/selenium/webdriver/remote/remote_connection.pyclient_config   s   zRemoteConnection.client_configc                 C      t jdtdd | jjS )zReturns timeout value in seconds for all http requests made to the Remote Connection.

        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      t jdtdd || j_dS )zuOverride 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-   )r3   r1   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/   r0   r   reset_timeoutr2   r&   r&   r'   r7      s   zRemoteConnection.reset_timeoutc                 C   r)   )a-  Returns paths of the .pem encoded certificate to verify connection to command executor.

        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/   r0   r   ca_certsr2   r&   r&   r'   get_certificate_bundle_path   s   	z,RemoteConnection.get_certificate_bundle_pathc                 C   r5   )zSet the path to the certificate bundle for verifying command executor connection.

        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+   Nr8   )r3   pathr&   r&   r'   set_certificate_bundle_path   s   	z,RemoteConnection.set_certificate_bundle_pathFc                 C   s|   dd| j d}|jr*tjddd t|j d|j  }|dd	|  i |r3|d
di | j	r<|| j	 |S )zGet headers for remote request.

        Args:
            parsed_url: The parsed url
            keep_alive: 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)r3   
parsed_url
keep_aliveheaders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 d S )NT)r   
_proxy_urlrC   rD   )r%   rI   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   rN   schemehostnameportrC   rD   )r%   rI   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r!d|d< ttjj	 n| j j
r/d|d< | j j
|d< | jrj| j drIdd	lm} || jfi |S |  r`|  \| _| _tjt| jd
|d< tj| jfi |S tjdi |S )Nr1   init_args_for_pool_manager	CERT_NONE	cert_reqsCERT_REQUIREDr9   sockr   )SOCKSProxyManager)proxy_basic_authproxy_headersr&   )r   r1   rF   rV   getignore_certificatesurllib3disable_warnings
exceptionsInsecureRequestWarningr9   rN   lower
startswithurllib3.contrib.socksr[   rO   rU   _basic_proxy_authmake_headersr   ProxyManagerPoolManager)r%   pool_manager_init_argsr[   r&   r&   r'   _get_connection_manager  s*   
z(RemoteConnection._get_connection_managerTremote_server_addrrJ   ignore_proxyr_   rV   r(   c                 C   s   |r|| _ n|rt||||d| _ ntd| j jt_| j jt_| j t_ | j jp*tjt_| j j	p2tj	t_	|r>t
jdtdd |sHt
jdtdd |rRt
jdtdd |r\t
jdtdd |rbd | _n| j  | _| j jrq|  | _t| _d S )	N)rm   rJ   r_   rV   z;Must provide either 'remote_server_addr' or 'client_config'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 instead)r   r	   r   r1   r   _timeoutr9   	_ca_certsrH   rB   r.   r/   r0   rN   get_proxy_urlrJ   rl   _connremote_commands	_commands)r%   rm   rJ   rn   r_   rV   r(   r&   r&   r'   __init__5  sZ   	




zRemoteConnection.__init__extra_commandsc                 C   s   ||f| j |< dS )zRegister a new command.N)rt   )r%   namemethodurlr&   r&   r'   add_commandx  s   zRemoteConnection.add_commandrw   c                 C   s   | j |S )z Retrieve a command if it exists.)rt   r^   )r%   rw   r&   r&   r'   get_command|  s   zRemoteConnection.get_commandc                 C   s   | j |p| j|}|dusJ d| |d }t||}dd |dD }t|tr<|r<|D ]}||= q6t	
|}| jj | }	| |}
td|d |	t|
 | j|d |	|d	S )
aq  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    c                 S   s"   h | ]}| d r|dd qS )$r|   Nre   ).0wordr&   r&   r'   	<setcomp>  s   " z+RemoteConnection.execute.<locals>.<setcomp>/z%s %s %sr   )body)rt   r^   rv   stringTemplate
substitutesplit
isinstancedictr   	dump_jsonr   rm   _trim_large_entriesLOGGERdebugstr_request)r%   commandparamscommand_infopath_stringr;   substitute_paramsr   datary   trimmedr&   r&   r'   execute  s   

zRemoteConnection.executereturnc              	   C   s~  t |}| || jj}| j }|r|| |r!|dvr!d}| jjr6| jj||||| jj	d}|j
}n%|  }	|	}
|
j||||| jj	d}W d   n1 sSw   Y  |j
}|jd}td|j
||j zd|  krvdk rn n| d|jd	dW td
 |  S |dkr|ddW td
 |  S |dkr||s|jn| dW td
 |  S g }|jddr|jddd}tdd |D s$z	t| }W n. ty   d|  k rdk rn ntj}ntj}|| d Y W td
 |  S w d|vrd|d< |W td
 |  S d|d}|W td
 |  S td
 |  w )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)r   rK   r1   zUTF-8z1Remote response: status=%s | data=%s | headers=%si,  i0  r   locationzFinished Requesti  zAuthorization Required)statusvaluei  r>   ;c                 S   s   g | ]}| d qS )z	image/pngr~   )r   xr&   r&   r'   
<listcomp>  s    z-RemoteConnection._request.<locals>.<listcomp>   r   r   )r   r   rM   r   rJ   get_auth_headerrF   rr   requestr1   r   rl   r   rG   r   r   rK   r   r^   closereasonstripr   anyr   	load_json
ValueErrorr   SUCCESSUNKNOWN_ERROR)r%   rx   ry   r   rI   rK   auth_headerresponse
statuscodeconnhttpr   content_typer   r&   r&   r'   r     sn   













	






zRemoteConnection._requestc                 C   s   t | dr| j  dS dS )z<Clean up resources when finished with the remote_connection.rr   N)hasattrrr   clearr$   r&   r&   r'   r     s   
zRemoteConnection.closed   c                 C   sj   i }|  D ],\}}t|tr| ||||< qt|tr.t||kr.|d| d ||< q|||< q|S )a   Truncate string values in a dictionary if they exceed max_length.

        Args:
            input_dict: Dictionary with potentially large values
            max_length: Maximum allowed length of string values

        Returns:
            Dictionary with truncated string values
        Nz...)itemsr   r   r   r   len)r%   
input_dict
max_lengthoutput_dictionarykeyr   r&   r&   r'   r     s   


z$RemoteConnection._trim_large_entries)F)NTFFNNr#   )r   )-__name__
__module____qualname____doc__r   r   __annotations__ossocketcertifigetdefaulttimeoutro   environgetenvwhererp   r	   sysplatformsystemrH   r   rB   propertyr(   classmethodr4   r6   r7   r:   r<   rM   rO   rU   rl   boolr   ru   rv   rz   r{   r   r   r   r   r&   r&   r&   r'   r      sr   
 



	


A>r   )wloggingr   r   r.   base64r   urllibr   urllib.parser   r   r`   seleniumr   selenium.common.exceptionsr   selenium.webdriver.remoter   'selenium.webdriver.remote.client_configr	   !selenium.webdriver.remote.commandr
   &selenium.webdriver.remote.errorhandlerr   	getLoggerr   r   NEW_SESSIONQUITW3C_GET_CURRENT_WINDOW_HANDLEW3C_GET_WINDOW_HANDLESr   
GO_FORWARDGO_BACKREFRESHW3C_EXECUTE_SCRIPTW3C_EXECUTE_SCRIPT_ASYNCGET_CURRENT_URL	GET_TITLEGET_PAGE_SOURCE
SCREENSHOTELEMENT_SCREENSHOTFIND_ELEMENTFIND_ELEMENTSW3C_GET_ACTIVE_ELEMENTFIND_CHILD_ELEMENTFIND_CHILD_ELEMENTSCLICK_ELEMENTCLEAR_ELEMENTGET_ELEMENT_TEXTSEND_KEYS_TO_ELEMENTGET_ELEMENT_TAG_NAMEIS_ELEMENT_SELECTEDIS_ELEMENT_ENABLEDGET_ELEMENT_RECTGET_ELEMENT_ATTRIBUTEGET_ELEMENT_PROPERTYGET_ELEMENT_ARIA_ROLEGET_ELEMENT_ARIA_LABELGET_SHADOW_ROOTFIND_ELEMENT_FROM_SHADOW_ROOTFIND_ELEMENTS_FROM_SHADOW_ROOTGET_ALL_COOKIES
ADD_COOKIE
GET_COOKIEDELETE_ALL_COOKIESDELETE_COOKIESWITCH_TO_FRAMESWITCH_TO_PARENT_FRAMESWITCH_TO_WINDOW
NEW_WINDOWCLOSE!GET_ELEMENT_VALUE_OF_CSS_PROPERTYEXECUTE_ASYNC_SCRIPTSET_TIMEOUTSGET_TIMEOUTSW3C_DISMISS_ALERTW3C_ACCEPT_ALERTW3C_SET_ALERT_VALUEW3C_GET_ALERT_TEXTW3C_ACTIONSW3C_CLEAR_ACTIONSSET_WINDOW_RECTGET_WINDOW_RECTW3C_MAXIMIZE_WINDOWSET_SCREEN_ORIENTATIONGET_SCREEN_ORIENTATIONGET_NETWORK_CONNECTIONSET_NETWORK_CONNECTIONGET_LOGGET_AVAILABLE_LOG_TYPESCURRENT_CONTEXT_HANDLECONTEXT_HANDLESSWITCH_TO_CONTEXTFULLSCREEN_WINDOWMINIMIZE_WINDOW
PRINT_PAGEADD_VIRTUAL_AUTHENTICATORREMOVE_VIRTUAL_AUTHENTICATORADD_CREDENTIALGET_CREDENTIALSREMOVE_CREDENTIALREMOVE_ALL_CREDENTIALSSET_USER_VERIFIEDUPLOAD_FILEGET_DOWNLOADABLE_FILESDOWNLOAD_FILEDELETE_DOWNLOADABLE_FILESFIRE_SESSION_EVENTGET_FEDCM_TITLEGET_FEDCM_DIALOG_TYPEGET_FEDCM_ACCOUNT_LISTCLICK_FEDCM_DIALOG_BUTTONCANCEL_FEDCM_DIALOGSELECT_FEDCM_ACCOUNTSET_FEDCM_DELAYRESET_FEDCM_COOLDOWNrs   r   r&   r&   r&   r'   <module>   s  
	
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHLMNRVWXYZ[]^_`h