a
    7ñ»jrj  ã                   @   s  d dl 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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 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Zdejv Zej d¡Ze   d¡Z!e! "e   ¡  #¡ ¡ G dd„ de
j$j%j$j&ƒZ'dd„ Z(dS )é    N)ÚFileLock)Úsuppressé   )ÚCDP©ÚPageElement)Ústart_detached)ÚChromeOptions)ÚReactor)Ú
WebElement)ÚChromer	   ÚPatcherr
   r   Úfind_chrome_executableÚdarwin)r   ÚcygwinÚlinuxZucc                       sê   e Zd ZdZeƒ ZdZdZd1‡ fdd„	Z‡ fdd	„Z	d
d„ Z
dd„ Zdd„ Zdd„ Z‡ fdd„Zdd„ Zdd„ Zd2‡ fdd„	Zdd„ Zdd„ Zd3dd „Zd!d"„ Zd#d$„ Zd4‡ fd%d&„	Zd'd(„ Z‡ fd)d*„Zd+d,„ Zd-d.„ Zd/d0„ Z‡  ZS )5r   zWControls chromedriver to drive a browser.
    The driver gets downloaded automatically.NFr   Tc           /         s6  || _ d| _ddl}ddlm} ddlm} |	rˆ| |jj¡}|�6 ddlm	} ||||
d�| _| j 
¡  W d  ƒ n1 s~0    Y  |s’tƒ }zTt|dƒrä|jdurät  d	¡ ttƒ� |j ¡  W d  ƒ n1 sÚ0    Y  W n tyø   Y n0 | |_d
}d}d}zVt ¡ �8}|jdddidd�}|jdk�r@tdƒ‚W d  ƒ n1 �sV0    Y  W n t�yz   d}Y n0 tj}d |¡}ddlm} dtjv �sÎd|v �sÎdtjv �sÎt|ddƒ�sÎ|�sÞtjjj j! "¡ }t|dƒ�rð|j#}|j$�sd||f |_$|�r| %ddddœ¡ | &d | ¡ | &d!| ¡ |�rZt'j( )|¡}| &d"| ¡ dt*|ƒ }}|j+D ]~}d#|v �r®t, -d$|¡} z| d }W n t.�y¬   d%}Y n0 d&|v �rnt, -d'|¡} z| d }d}W n t.�yè   Y n0 �qn|�sFt|d(dƒ�r| &d"|j/ ¡ d}n.ddl0}!t'j( 1|! 2¡ ¡}d}d"| }| &|¡ |�s°ttƒ�, ddl3}"|" 4¡ d  5d)d*¡}W d  ƒ n1 �sˆ0    Y  |�r¬d+|v �s¬d,|v �r°d-}| &d.| ¡ |j6�sè|�pÐt7ƒ }#|#�rà|#|_6ntd/ƒ‚|j8j9| _:|| _/|| _;|�r|j+ <g d0¢¡ | &d1| �p6t=t> ?¡  @¡ d2ƒd ¡ t|d3ƒ�rP| A|¡ ttƒ�¸ ddlB}$tCt'j( t'j( )|¡d4d5¡d6d7d8d9��j}%|$ D|%¡}&d:|&d;  E¡ v�s¶|&d; d: du�rÂd|&d; d:< |% Fdd¡ |% G¡  |$ H|&|%¡ W d  ƒ n1 �sø0    Y  W d  ƒ n1 �s0    Y  d}'d<tjIv �r8tJjK}'|| _L| |jj¡}|��Î |�srtM|j6g|j+¢R Ž | _NnntO|jjPƒ}(|(�L | Q|jjP¡ tJjR|j6g|j+¢tJjStJjStJjStT|'d=�})|)jU| _NW d  ƒ n1 �sÖ0    Y  d}*tJjS}+|	�rtjjVj jW| jjXd>g||+d?�}*ntjjVj jW|d>g||+d?�}*t|*d@ƒ�rBtY|*d@|'ƒ t|*dAƒ�rZtY|*dA|'ƒ ztZƒ j[||*dB� W nV t\�yÄ }, z<t]�r®dCt^|,ƒv �r®t_t^|,ƒƒ dD}-t|-ƒ‚n‚ W Y d},~,n
d},~,0 0 d| _`|�rt> ?¡  @¡ t>jak�röt> ?dE¡ bdF¡ tc| ƒ}.|. d¡  |.| _`te| _fW d  ƒ n1 �s(0    Y  dS )GaÁ  
        Starts the Chrome service and creates a new instance of chromedriver.

        Parameters
        ----------

        options: (default: None)
            Takes an instance of ChromeOptions to customize browser behavior.

        user_data_dir:
            None (default) Create a temp profile directory for the browser.
            If user_data_dir is a path to a valid Chrome profile directory,
            use it and turn off the automatic removal mechanism at exit.

        driver_executable_path:
            None (default) Downloads and patches the new binary.

        browser_executable_path:
            None (default) Use find_chrome_executable().
            (If not specified, make sure Chrome is on the PATH.)

        port: (default: 0)
            Port you would like the service to run.
            If left as 0, a free port will be found.

        enable_cdp_events: (default: False)
            This enables the handling of wire messages.
            When enabled, you can subscribe to CDP events by using:

                driver.add_cdp_listener("Network.dataReceived", yourcallback)
                # yourcallback: callable that accepts exactly 1 dict parameter.

        log_level: (default: adapts to python global log level)

        headless: (default: False)
            Use headless mode.
            (Already handled by seleniumbase/core/browser_launcher.py)

        patch_driver: (default: True)
            Patches uc_driver to be undetectable if not already patched.

        version_main: (default: None)
            Overrides the browser version for older versions of Chrome.
            Eg: version_main=96
            (Useful when you have a newer driver, but an older browser.)

        patcher_force_close: (default: False)
            Instructs patcher to access the chromedriver binary.
            If the file is locked, it will force shutdown all instances.
            Setting this is not recommended, unless you know the implications.

        suppress_welcome: (default: True)
            Suppress the Chrome welcome screen that appears on first-time runs.

        use_subprocess: (default: True)
            Subprocess chromedriver/python: Don't make Chrome a parent process.
        Nr   )Ú	constants)Úshared_utilsr   )r   )Úexecutable_pathÚforceÚversion_mainÚ_sessionz)You cannot reuse the ChromeOptions objectz	127.0.0.1i$  Fzhttp://127.0.0.1:9222Ú
ConnectionÚcloseé   )ÚheadersÚtimeoutéÈ   z"The port is free! It will be used!Tú )Úconfigz-nz -n=z-cZmulti_proxyÚ_remote_debugging_portz%s:%dzgoog:loggingPrefsZALL)ZperformanceÚbrowserz--remote-debugging-host=%sz--remote-debugging-port=%sz--user-data-dir=%sÚlangz(?:--)?lang(?:[ =])?(.*)zen-US,en;q=0.9zuser-data-dirz!(?:--)?user-data-dir(?:[ =])?(.*)Úuser_data_dirÚ_ú-ZEnglishzUnited Stateszen-USz	--lang=%sz#Chrome not found! Install it first!)z--no-default-browser-checkz--no-first-runz--no-service-autorunz--password-store=basicz--profile-directory=Defaultz--log-level=%dé
   Úhandle_prefsZDefaultZPreferenceszutf-8zr+Úignore)ÚencodingÚmodeÚerrorsZ	exit_typeZprofileÚwin32)ÚstdinÚstdoutÚstderrÚ	close_fdsÚcreationflagsz--disable-build-check)r   Zservice_argsÚportÚ
log_outputr1   Zcreation_flags)ÚoptionsÚservicezBad CPU type in executablez¢Missing a macOS dependency:
Your Mac needs Rosetta 2 to use UC Mode!
Run: "softwareupdate --install-rosetta"
Info: https://apple.stackexchange.com/a/408379/607628z+selenium.webdriver.remote.remote_connectioné   )gÚdebugÚpatcherÚ	fastenersZseleniumbase.fixturesr   r   ZInterProcessLockZMultiBrowserZDRIVER_FIXING_LOCKr   Úautor	   Úhasattrr   Úloggerr   Ú	ExceptionÚquitÚAttributeErrorÚrequestsÚSessionÚgetÚstatus_codeÚsysÚargvÚjoinZseleniumbaser   ÚgetattrÚseleniumÚ	webdriverÚcommonr5   ÚutilsZ	free_portr    Údebugger_addressZset_capabilityÚadd_argumentÚosÚpathÚabspathÚboolÚ	argumentsÚreÚsearchÚ
IndexErrorr#   ÚtempfileÚnormpathÚmkdtempÚlocaleÚ	getlocaleÚreplaceÚbinary_locationr   ZUCZRECONNECT_TIMEÚ_delayÚkeep_user_data_dirÚextendÚdivmodÚloggingÚ	getLoggerÚgetEffectiveLevelr'   ÚjsonÚopenÚloadÚkeysÚseekÚtruncateÚdumpÚplatformÚ
subprocessÚCREATE_NO_WINDOWr4   r   Úbrowser_pidr   ZPYAUTOGUILOCKZmake_writableÚPopenÚPIPEÚIS_POSIXÚpidÚchromeZServicer   ÚsetattrÚsuperÚ__init__ÚOSErrorÚIS_MACÚstrÚprintÚreactorÚDEBUGÚsetLevelr
   Ústartr   Z_web_element_cls)/Úselfr4   r#   Zdriver_executable_pathZbrowser_executable_pathr2   Zenable_cdp_eventsZ	log_levelZheadlessZpatch_driverr   Zpatcher_force_closeZsuppress_welcomeZuse_subprocessr7   Úkwr9   r   r   Zuc_lockr   Z
debug_hostZ
debug_portZspecial_port_freeÚsessionÚresZsys_argvZarg_joinZ	sb_configÚlanguager^   ÚargÚmrV   rY   r\   rd   Úfsr   r1   Zgui_lockr!   Zservice_r3   ÚeÚmessager{   ©Ú	__class__© úFE:\soccer\ml_env\Lib\site-packages\seleniumbase/undetected/__init__.pyrv   *   s®   Kÿý(

,
ý,

ÿÿÿ
þý
ÿ




ÿ

4ÿþý
ÿ
ÿ	
þ


ýø	
ÿþLÿÿÿÿú(
ü
üÿ
ÿþzChrome.__init__c                    sX   t ƒ  d¡st ƒ  |¡S dd l}t ƒ  |¡‰ | ˆ ¡rP| ˆ ¡sP‡ fdd„}|S ˆ S d S )Nr7   r   c                     s   ˆ | i |¤ŽS ©Nr‹   )ÚargsÚkwargs©Úoriginalr‹   rŒ   Únewfuncc  s    z(Chrome.__getattribute__.<locals>.newfunc)ru   Ú__getattribute__ÚinspectÚismethodÚisclass)r   Úitemr”   r’   r‰   r�   rŒ   r“   \  s    zChrome.__getattribute__c                 C   s
   t  | ¡S r�   )ÚobjectÚ__dir__©r   r‹   r‹   rŒ   r™   h  s    zChrome.__dir__c                 C   s:   g }t tƒ� |  d¡}W d   ƒ n1 s,0    Y  |S )Na«  
                let objectToInspect = window,
                    result = [];
                while(objectToInspect !== null)
                { result = result.concat(
                    Object.getOwnPropertyNames(objectToInspect)
                  );
                  objectToInspect = Object.getPrototypeOf(objectToInspect); }
                return result.filter(i => i.match(/^[a-z]{3}_[a-z]{22}_.*/i))
                )r   r=   Zexecute_script©r   Ú	cdc_propsr‹   r‹   rŒ   Ú_get_cdc_propsk  s    
ÿ"zChrome._get_cdc_propsc                 C   sD   t |ƒdk rd S dd dd„ |D ƒ¡ d }|  dd|d	 i¡ d S )
Nr   ú[z, c                 s   s   | ]}d | d  V  qdS )ú"Nr‹   )Ú.0Úpr‹   r‹   rŒ   Ú	<genexpr>  s   z0Chrome._hook_remove_cdc_props.<locals>.<genexpr>ú]z%Page.addScriptToEvaluateOnNewDocumentÚsourcez .forEach(p => delete window[p]);)ÚlenrF   Zexecute_cdp_cmd)r   rœ   Zcdc_props_js_arrayr‹   r‹   rŒ   Ú_hook_remove_cdc_props|  s    ÿþÿÿþzChrome._hook_remove_cdc_propsc                 C   s,   |   ¡ }t|ƒdkr(|  |¡ t d¡ d S )Nr   çš™™™™™©?)r�   r¥   r¦   ÚtimeÚsleepr›   r‹   r‹   rŒ   Úremove_cdc_props_as_needed‹  s    
z!Chrome.remove_cdc_props_as_neededc                    s   |   ¡  tƒ  |¡S r�   )rª   ru   rB   ©r   Úurlr‰   r‹   rŒ   rB   ‘  s    z
Chrome.getc                 C   s2   t | dd ƒr.t| jtƒr.| j ||¡ | jjS dS )Nr{   F)rG   Ú
isinstancer{   r
   Zadd_event_handlerÚhandlers)r   Z
event_nameÚcallbackr‹   r‹   rŒ   Úadd_cdp_listener•  s    
ÿ
þzChrome.add_cdp_listenerc                 C   s(   t | dd ƒr$t| jtƒr$| jj ¡  d S )Nr{   )rG   r­   r{   r
   r®   Úclearrš   r‹   r‹   rŒ   Úclear_cdp_listenersž  s
    
ÿ
þzChrome.clear_cdp_listenersc                    s6   |   tjjjjjddi¡ |r2|  ¡  tƒ  	|¡S d S )NÚtypeÚwindow)
ÚexecuterH   rI   ÚremoteÚcommandÚCommandZ
NEW_WINDOWrª   ru   rB   r«   r‰   r‹   rŒ   Ú
window_new¥  s    þzChrome.window_newc                 C   s*   t | dƒst| jƒ| _| j t|ƒ¡ dS )zOpen url in a new tab.ÚcdpN)r;   r   r4   rº   Útab_newry   r«   r‹   r‹   rŒ   r»   ¯  s    
zChrome.tab_newc                 C   s6   t | dƒst| jƒ| _|  | jjd ¡}dd„ |D ƒS )Nrº   Úlistc                 S   s   g | ]}t |ƒ‘qS r‹   r   )r    Úor‹   r‹   rŒ   Ú
<listcomp>º  ó    z#Chrome.tab_list.<locals>.<listcomp>)r;   r   r4   rº   rB   Z	endpoints)r   Úretvalr‹   r‹   rŒ   Útab_listµ  s    
zChrome.tab_listçš™™™™™¹?c                 C   sÞ  t | dƒ�r0ttƒ�° | j ¡ r¶|  ¡  zXz| j ¡  W n tyJ   Y n0 W ttƒ� | j ¡  W d  ƒ q¶1 sv0    Y  n4ttƒ� | j ¡  W d  ƒ n1 sª0    Y  0 W d  ƒ n1 sÊ0    Y  t	|t
ƒrò| ¡ dkrütƒ  n
t |¡ ttƒ� | j ¡  W d  ƒ n1 �s&0    Y  ttƒ�" |  ¡  t d¡ W d  ƒ n1 �sb0    Y  ttƒ�� | jD ]ø}| j |¡ | j d¡�r~dtjv �r°|  ¡  | j ¡ �rX|  ¡  z\z| j ¡  W n t�yè   Y n0 W ttƒ� | j ¡  W d  ƒ n1 �s0    Y  n6ttƒ� | j ¡  W d  ƒ n1 �sL0    Y  0 | j ¡  |  ¡  t d¡ �q~W d  ƒ n1 �sŽ0    Y  ttƒ�" | j | jd ¡ W d  ƒ n1 �sÊ0    Y  d	| _dS )
zèThis can be useful when sites use heavy detection methods:
        - Stops the chromedriver service that runs in the background.
        - Starts the chromedriver service that runs in the background.
        - Recreates the session.r5   NÚ
breakpointç¸…ëQ¸~?úchrome-extension://r   çú~j¼t“h?éÿÿÿÿT)r;   r   r=   r5   Úis_connectableÚstop_clientÚsend_remote_shutdown_commandÚ	TypeErrorÚ_terminate_processr­   ry   ÚlowerrÃ   r¨   r©   r~   Ústart_sessionÚwindow_handlesÚ	switch_tor´   Úcurrent_urlÚ
startswithrD   rk   r   Ú_is_connected)r   r   Úwindow_handler‹   r‹   rŒ   Ú	reconnect¼  sX    


*ÿ
H


*
*

,ÿ
,
.
2zChrome.reconnectc                 C   sæ   t | dƒrÜttƒ�º | j ¡ r¾|  ¡  t d¡ zXz| j ¡  W n t	yR   Y n0 W ttƒ� | j 
¡  W d  ƒ q¾1 s~0    Y  n4ttƒ� | j 
¡  W d  ƒ n1 s²0    Y  0 W d  ƒ n1 sÒ0    Y  d| _dS )zStops the chromedriver service that runs in the background.
        To use driver methods again, you MUST call driver.connect()r5   rÆ   NF)r;   r   r=   r5   rÈ   rÉ   r¨   r©   rÊ   rË   rÌ   rÓ   rš   r‹   r‹   rŒ   Ú
disconnectð  s    




*ÿ
HzChrome.disconnectc                 C   s:  t | dƒr<ttƒ� | j ¡  W d  ƒ n1 s20    Y  ttƒ�" |  ¡  t d¡ W d  ƒ n1 sl0    Y  ttƒ��b | jD �]H}| j	 
|¡ d}t | dƒræt | jdƒrættƒ� | j ¡ }W d  ƒ n1 sÜ0    Y  |sð| j}| d¡rˆdtjv �r|  ¡  | j ¡ �r¶|  ¡  z\z| j ¡  W n t�yF   Y n0 W ttƒ� | j ¡  W d  ƒ n1 �st0    Y  n6ttƒ� | j ¡  W d  ƒ n1 �sª0    Y  0 | j ¡  |  ¡  t d¡ qˆW d  ƒ n1 �sê0    Y  ttƒ�" | j	 
| jd	 ¡ W d  ƒ n1 �s&0    Y  d
| _dS )z^Starts the chromedriver service that runs in the background
        and recreates the session.r5   NrÄ   rº   ZdriverrÅ   r   rÆ   rÇ   T)r;   r   r=   r5   r~   rÎ   r¨   r©   rÏ   rÐ   r´   rº   Zget_current_urlrÑ   rÒ   rD   rk   r   rÈ   rÉ   rÊ   rË   rÌ   rÓ   )r   rÔ   rÑ   r‹   r‹   rŒ   Úconnect  sD    

(
(
(

,ÿ
,
,
2zChrome.connectc                    s   |s| j  ¡ }tƒ  |¡ d S r�   )r4   Zto_capabilitiesru   rÎ   )r   Zcapabilitiesr‰   r‹   rŒ   rÎ   )  s    
zChrome.start_sessionc                 C   sZ  zJt  d¡ t | jd¡ dtjv r>t | jd¡ t 	d¡ n
t 	d¡ W nd t
tttfyn   t 	d¡ Y nB tyž } zt j|dd	� W Y d }~nd }~0  ty®   Y n0 ttƒ� |  ¡  W d   ƒ n1 sÖ0    Y  ttƒ�. t| d
ƒ�r| j�r| j ¡  W d   ƒ n1 �s0    Y  tj | ¡ t| dƒ�rt| jdd ƒ�rt  d¡ ttƒ�¤ z\z| j ¡  W n t�yˆ   Y n0 W ttƒ� | j ¡  W d   ƒ n1 �s¶0    Y  n6ttƒ� | j ¡  W d   ƒ n1 �sì0    Y  0 W d   ƒ n1 �s0    Y  t| dƒ�rŽ| j�rŽt| jdƒ�rŽt  d¡ ttƒ�* | jj ¡  | jj dd� W d   ƒ n1 �s~0    Y  d | _t| dƒ�rPt| dƒ�rP| j!�sPdd l"}t#dƒD ]�}z|j$| j%dd� W nT t&�yê   Y nX ttt'f�y* } z"t  d|j(j)|f ¡ W Y d }~n d }~0 0 t  d| j% ¡  �qPt 	d¡ �q¾d | _*d S )NzTerminating the UC browseré   r   r   g{®Gáz”?g{®Gáz¤?r§   T)Úexc_infoÚcommand_executorr5   ÚprocesszStopping webdriver servicer{   ÚeventzShutting down Reactorr   ©r   r^   r#   é   F)Úignore_errorsz<When removing the temp profile, a %s occured: %s
Retrying...zSuccessfully removed %srÂ   )+r<   r7   rN   Úkillrn   rD   rk   Úwaitpidr¨   r©   r?   ÚChildProcessErrorÚRuntimeErrorrw   ÚTimeoutErrorr=   r   rÉ   r;   rÚ   r   r   Ú
_instancesÚdiscardrG   r5   rÊ   rË   rÌ   r{   rÜ   ÚsetrF   r^   ÚshutilÚrangeÚrmtreer#   ÚFileNotFoundErrorÚPermissionErrorrŠ   Ú__name__r8   )r   r‡   rè   r$   r‹   r‹   rŒ   r>   .  s‚    

"
&
*


,ÿ
Lÿþ
ý

.ÿþý
þÿÿzChrome.quitc                    s„   t tƒ�8 dtjv r(|  ¡  | j ¡  n
tƒ  ¡  W d   ƒ n1 sF0    Y  t tƒ� |  ¡  W d   ƒ n1 sv0    Y  d S )Nr,   )	r   r=   rD   rk   rÉ   rÚ   r   ru   r>   rš   r‰   r‹   rŒ   Ú__del__u  s    

(
zChrome.__del__c                 C   s   | S r�   r‹   rš   r‹   r‹   rŒ   Ú	__enter__  s    zChrome.__enter__c                 C   s   | j | jd� d S )NrÝ   )rÕ   r]   )r   Úexc_typeÚexc_valÚexc_tbr‹   r‹   rŒ   Ú__exit__‚  s    zChrome.__exit__c                 C   s   t | jjƒS r�   )Úhashr4   rL   rš   r‹   r‹   rŒ   Ú__hash__…  s    zChrome.__hash__)NNNNr   Fr   FTNFTTF)N)rÂ   )N)rí   Ú
__module__Ú__qualname__Ú__doc__rç   rå   Z
session_idr7   rv   r“   r™   r�   r¦   rª   rB   r°   r²   r¹   r»   rÁ   rÕ   rÖ   r×   rÎ   r>   rî   rï   ró   rõ   Ú__classcell__r‹   r‹   r‰   rŒ   r   #   sR                 ñ  4	

4(G
r   c               	   C   s  ddl m}  |  dd¡}tj |¡r>t |tj¡r>tj |¡S t	ƒ }t
r tj d¡ tj¡D ]$}dD ]}| tj ||f¡¡ qdq\dtjv rÖd}| |d	g¡ n6ttjjd
ƒD ]&}dD ]}| tj ||df¡¡ q¶q®|D ].}tj |¡rÚt |tj¡rÚtj |¡  S qÚd S )Nr   )Údetect_b_verúgoogle-chromeTÚPATH)rû   zgoogle-chrome-stablezgoogle-chrome-betazgoogle-chrome-devzgoogle-chrome-unstablers   Zchromiumzchromium-browserr   z</Applications/Google Chrome.app/Contents/MacOS/Google Chromez2/Applications/Chromium.app/Contents/MacOS/Chromium)ZPROGRAMFILESzPROGRAMFILES(X86)ÚLOCALAPPDATAZPROGRAMW6432)zGoogle/Chrome/ApplicationzGoogle/Chrome Beta/Applicationz Google/Chrome Canary/Applicationz
chrome.exe)Zseleniumbase.corerú   Zget_binary_locationrN   rO   ÚexistsÚaccessÚX_OKrW   rç   rq   ÚenvironrB   ÚsplitÚpathsepÚaddÚseprF   rD   rk   ÚupdateÚmap)rú   r\   Ú
candidatesr—   ÚsubitemÚgcÚ	candidater‹   r‹   rŒ   r   ‰  s2    

ÿÿþ	r   ))ra   rN   rS   r@   rl   rD   r¨   Úfilelockr   Z!selenium.webdriver.chrome.servicerH   Z#selenium.webdriver.chrome.webdriverZ!selenium.webdriver.common.serviceZ!selenium.webdriver.remote.commandÚ
contextlibr   rº   r   r   Zdprocessr   r4   r	   r{   r
   Z
webelementr   Ú__all__rk   rx   rÒ   rq   rb   r<   r}   rc   rI   rs   Z	WebDriverr   r   r‹   r‹   r‹   rŒ   Ú<module>   s:   

    j