a
    .c#                     @   sV   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
Z
G dd dZdS )    N)system)PopenSTDOUT)WebDriverException)utilsc                   @   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   sz   || _ |pttjd| _d| _t  | _| j s:| 	 | _ | j 
 sLt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.
        wbNzFailed 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_cmdopenosdevnull	_log_filecommand_liner   lowerplatform_get_firefox_start_cmdstripr   environcopy_firefox_env)selfZfirefox_pathZlog_file r   c/var/www/brookimports/venv/lib/python3.9/site-packages/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_options@   s    z&FirefoxBinary.add_command_line_options   c                 C   s$   || _ | | j j | j|d dS )zkLaunches 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_browserC   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%   L   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    U   s    



z&FirefoxBinary._start_from_profile_pathc                 C   s\   d}t | jjsX| j r$td||krD|   td| jj |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.zCan't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: %s 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"   b   s     
z%FirefoxBinary._wait_until_connectablec           
      C   s   zddl m}m}m}m} W n* tyF   ddlm}m}m}m} Y n0 dd l}d}d}|D ]j}z|||}	||	d}W  qW q\ ty   z |||}	||	d}W Y  qW n ty   Y n0 Y q\0 q\dS |sdS |	|d S )Nr   )OpenKey
QueryValueHKEY_LOCAL_MACHINEHKEY_CURRENT_USER)z/SOFTWARE\Classes\FirefoxHTML\shell\open\commandz<SOFTWARE\Classes\Applications\firefox.exe\shell\open\command )
_winregr5   r6   r7   r8   ImportErrorwinregshlexOSErrorsplit)
r   r5   r6   r7   r8   r=   keysr-   r!   keyr   r   r   _find_exe_in_registryx   s.    




z#FirefoxBinary._find_exe_in_registryc                 C   s   d}| j dkrBd}| |}|s$d}tj|stjd| }n^| j dkr^|  pZ|  }nB| j dkr|tjdkr||  }n$d	D ]}| |}|r qqt	d
|S )z$Return the command to start firefox.r9   darwinfirefoxz4/Applications/Firefox.app/Contents/MacOS/firefox-bin~windowsjavant)rD   Z	iceweaselziCould not find firefox in your system PATH. Please specify the firefox binary location or install firefox)
r   whichr   r!   exists
expanduserrB   _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.exer9   )r   getenvr!   joinaccessX_OK)r   Zprogram_filesr!   Zbinary_pathr   r   r   rL      s    


z'FirefoxBinary._default_windows_locationc                 C   sD   t jdd}| | j| jdd}||7 }|| jd< | j| jd< d S )NZLD_LIBRARY_PATHr9   x86amd64Z
LD_PRELOAD)r   r   get_extract_and_checkr   NO_FOCUS_LIBRARY_NAMEr   )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 )Nr9   r   :)
r   r!   rP   rJ   makedirsshutilr   dirname__file__rW   )
r   r   Zno_focus_so_namerS   rT   pathsZ
built_pathr!   Zlibrary_pathrZ   r   r   r   rV      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 )zLReturns the fully qualified path by searching Path of the given
        namePATHN)	r   r   r?   pathsepr!   rP   rQ   rR   isdir)r   fnamepeZ	checknamer   r   r   rI      s
    
zFirefoxBinary.which)NN)r   )r   )__name__
__module____qualname__rW   r   r   r#   r%   r    r"   rB   r   rL   r+   rV   rI   r   r   r   r   r      s   
!
		
	r   )r   r   r   
subprocessr   r   Zselenium.common.exceptionsr   Zselenium.webdriver.commonr   r2   r   r   r   r   r   <module>   s   