U
    PÖýf¤#  ã                   @   s~   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 e	d	ƒG d
d„ dƒƒZdS )é    N)Úsystem)ÚDEVNULL)ÚSTDOUT)ÚPopen)Ú
deprecated)ÚWebDriverException)Úutilsz?Use binary_location property in Firefox Options to set locationc                   @   sv   e Zd ZdZddd„Zdd„ Zddd	„Zd
d„ Zdd„ Zddd„Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚFirefoxBinaryzx_ignore_nofocus.soNc                 C   sr   || _ |pt| _d| _tƒ  ¡ | _| j s2|  ¡ | _ | j  ¡ sDt	dƒ‚t
j ¡ | _d| jd< d| jd< d| jd< dS )a¾  Creates a new instance of Firefox binary.

        :Args:
         - firefox_path - Path to the Firefox executable. By default, it will be detected from the standard locations.
         - log_file - A file object to redirect the firefox process output to. It can be sys.stdout.
                      Please note that with parallel run the output won't be synchronous.
                      By default, it will be redirected to /dev/null.
        NzûFailed to find firefox binary. You can set it by specifying the path to 'firefox_binary':

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('/path/to/binary')
driver = webdriver.Firefox(firefox_binary=binary)Ú1ZMOZ_CRASHREPORTER_DISABLEZMOZ_NO_REMOTEZNO_EM_RESTART)Ú
_start_cmdr   Ú	_log_fileÚcommand_liner   ÚlowerÚplatformÚ_get_firefox_start_cmdÚstripr   ÚosÚenvironÚcopyÚ_firefox_env)ÚselfZfirefox_pathZlog_file© r   úM/tmp/pip-unpacked-wheel-ds5t_0qj/selenium/webdriver/firefox/firefox_binary.pyÚ__init__$   s    	


ÿ


zFirefoxBinary.__init__c                 G   s
   || _ d S )N)r   )r   Úargsr   r   r   Úadd_command_line_optionsE   s    z&FirefoxBinary.add_command_line_optionsé   c                 C   s$   || _ |  | j j¡ | j|d dS )zlLaunches the browser for the given profile name.

        It is assumed the profile already exists.
        )ÚtimeoutN)ÚprofileÚ_start_from_profile_pathÚpathÚ_wait_until_connectable)r   r   r   r   r   r   Úlaunch_browserH   s    zFirefoxBinary.launch_browserc                 C   s   | j r| j  ¡  | j  ¡  dS )zMKill the browser.

        This is useful when the browser is stuck.
        N)ÚprocessÚkillÚwait)r   r   r   r   r$   R   s    
zFirefoxBinary.killc                 C   s\   || j d< | jdkr|  ¡  | jdg}| jrB| jD ]}| |¡ q2t|| jt| j d| _	d S )NZXRE_PROFILE_PATHÚlinuxz-foreground)ÚstdoutÚstderrÚenv)
r   r   Ú_modify_link_library_pathr   r   Úappendr   r   r   r#   )r   r    ÚcommandÚclir   r   r   r   [   s    



z&FirefoxBinary._start_from_profile_pathc                 C   s`   d}t  | jj¡s\| j ¡ r$tdƒ‚||krH|  ¡  td| jj› dƒ‚|d7 }t	 
d¡ qdS )z9Blocks until the extension is connectable in the firefox.r   zThe browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.zzCan't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: zT If you specified a log_file in the FirefoxBinary constructor, check it for details.é   T)r   Zis_connectabler   Úportr#   Úpollr   r$   r    ÚtimeÚsleep)r   r   Úcountr   r   r   r!   f   s    
ÿÿz%FirefoxBinary._wait_until_connectablec           
      C   s   z4ddl m} ddl m} ddl m} ddl m} W n, tk
r`   ddlm}m}m}m} Y nX dd l}d}d}|D ]n}z|||ƒ}	||	dƒ}W  qêW qv tk
râ   z |||ƒ}	||	dƒ}W Y  qêW n tk
rÜ   Y nX Y qvX qvdS |sòdS | 	|¡d S )	Nr   )ÚHKEY_CURRENT_USER)ÚHKEY_LOCAL_MACHINE)ÚOpenKey)Ú
QueryValue)r6   r7   r5   r4   )z/SOFTWARE\Classes\FirefoxHTML\shell\open\commandz<SOFTWARE\Classes\Applications\firefox.exe\shell\open\commandÚ )
Ú_winregr4   r5   r6   r7   ÚImportErrorÚwinregÚshlexÚOSErrorÚsplit)
r   r4   r5   r6   r7   r<   Úkeysr,   r    Úkeyr   r   r   Ú_find_exe_in_registry}   s4    




z#FirefoxBinary._find_exe_in_registryc                 C   s¤   d}| j dkrBd}|  |¡}|s$d}tj |¡s tj d¡| }n^| j dkr^|  ¡ pZ|  ¡ }nB| j dkr|tjdkr||  ¡ }n$d	D ]}|  |¡}|r€ q q€t	d
ƒ‚|S )z$Return the command to start firefox.r8   ÚdarwinÚfirefoxz0/Applications/Firefox.app/Contents/MacOS/firefoxú~ZwindowsÚjavaÚnt)rC   Z	iceweaselziCould not find firefox in your system PATH. Please specify the firefox binary location or install firefox)
r   Úwhichr   r    ÚexistsÚ
expanduserrA   Ú_default_windows_locationÚnameÚRuntimeError)r   Z	start_cmdZffnamer   r   r   r       s(    




ÿz$FirefoxBinary._get_firefox_start_cmdc                 C   sJ   t  dd¡t  dd¡g}|D ](}t j |d¡}t  |t j¡r|  S qdS )NZPROGRAMFILESzC:\Program FileszPROGRAMFILES(X86)zC:\Program Files (x86)zMozilla Firefox\firefox.exer8   )r   Úgetenvr    ÚjoinÚaccessÚX_OK)r   Zprogram_filesr    Zbinary_pathr   r   r   rJ   ½   s    

þ
z'FirefoxBinary._default_windows_locationc                 C   s@   t j dd¡}|  | jdd¡}||7 }|| jd< | j| jd< d S )NZLD_LIBRARY_PATHr8   Úx86Úamd64Z
LD_PRELOAD)r   r   ÚgetÚ_extract_and_checkr   r   ÚNO_FOCUS_LIBRARY_NAME)r   Zexisting_ld_lib_pathZnew_ld_lib_pathr   r   r   r*   È   s
    
z'FirefoxBinary._modify_link_library_pathc           	      C   sv   ||g}d}|D ]`}t j |j|¡}t j |¡s:t  |¡ dd l}| t j t j t¡|| j	¡|¡ ||d 7 }q|S )Nr8   r   ú:)
r   r    rN   rH   ÚmakedirsÚshutilr   ÚdirnameÚ__file__rU   )	r   r   rQ   rR   ÚpathsZ
built_pathr    Zlibrary_pathrX   r   r   r   rT   Ò   s    
"z FirefoxBinary._extract_and_checkc                 C   sL   t jd  t j¡D ]4}t j ||¡}t  |t j¡rt j |¡s|  S qdS )zMReturns the fully qualified path by searching Path of the given
        name.ÚPATHN)	r   r   r>   Úpathsepr    rN   rO   rP   Úisdir)r   ÚfnameÚpeZ	checknamer   r   r   rG   à   s
    
zFirefoxBinary.which)NN)r   )r   )Ú__name__Ú
__module__Ú__qualname__rU   r   r   r"   r$   r   r!   rA   r   rJ   r*   rT   rG   r   r   r   r   r	       s   
!

	
#
r	   )r   r1   r   r   Ú
subprocessr   r   r   Ztyping_extensionsr   Zselenium.common.exceptionsr   Zselenium.webdriver.commonr   r	   r   r   r   r   Ú<module>   s   