o
    US`c4                     @   s   d dl Z d dlmZ d dlZd dlmZ d dl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 ddlmZ ddlmZmZ dZdZG dd deZdS )    N)BytesIO)rmtree)contextmanager)DesiredCapabilities)	WebDriver   )FirefoxBinary)FirefoxProfile)Options)FirefoxRemoteConnection)DEFAULT_EXECUTABLE_PATHServicezgeckodriver.logc                       s   e Zd ZdZdZddddededddedf fdd	Zd fdd	Z	e
d
d ZdddZedd ZddefddZdddZdefddZdefddZdefddZdefddZ  ZS )r   ZchromecontentNTc                    s  |t krtjdtdd |s|
rtjdtdd |r"tjdtdd d| _|r/tjdtdd d| _|tkr>tjdtdd |tkrJtjd	tdd |rTtjd
tdd |	| _|s]|
r]|
}|sdt	j
 }|sit }t|}|drw|d |_|r|jr|j| _|jr|j| _|rt|trt|}|| _||_|rt|trt|}|| _||_|dr|jsd|_| jst|||d| _| j  t| jj|jd}t j||dd d| _dS )a
  Starts a new local session of Firefox.

        Based on the combination and specificity of the various keyword
        arguments, a capabilities dictionary will be constructed that
        is passed to the remote end.

        The keyword arguments given to this constructor are helpers to
        more easily allow Firefox WebDriver sessions to be customised
        with different options.  They are mapped on to a capabilities
        dictionary that is passed on to the remote end.

        As some of the options, such as `firefox_profile` and
        `options.profile` are mutually exclusive, precedence is
        given from how specific the setting is.  `capabilities` is the
        least specific keyword argument, followed by `options`,
        followed by `firefox_binary` and `firefox_profile`.

        In practice this means that if `firefox_profile` and
        `options.profile` are both set, the selected profile
        instance will always come from the most specific variable.
        In this case that would be `firefox_profile`.  This will result in
        `options.profile` to be ignored because it is considered
        a less specific setting than the top-level `firefox_profile`
        keyword argument.  Similarly, if you had specified a
        `capabilities["moz:firefoxOptions"]["profile"]` Base64 string,
        this would rank below `options.profile`.

        :param firefox_profile: Deprecated: Instance of ``FirefoxProfile`` object
            or a string.  If undefined, a fresh profile will be created
            in a temporary location on the system.
        :param firefox_binary: Deprecated: Instance of ``FirefoxBinary`` or full
            path to the Firefox binary.  If undefined, the system default
            Firefox installation will  be used.
        :param capabilities: Deprecated: Dictionary of desired capabilities.
        :param proxy: Deprecated: The proxy settings to use when communicating with
            Firefox via the extension connection.
        :param executable_path: Deprecated: Full path to override which geckodriver
            binary to use for Firefox 47.0.1 and greater, which
            defaults to picking up the binary from the system path.
        :param options: Instance of ``options.Options``.
        :param service_log_path: Deprecated: Where to log information from the driver.
        :param service_args: Deprecated: List of args to pass to the driver service
        :param desired_capabilities: Deprecated: alias of capabilities. In future
            versions of this library, this will replace 'capabilities'.
            This will make the signature consistent with RemoteWebDriver.
        :param keep_alive: Whether to configure remote_connection.RemoteConnection to use
             HTTP keep-alive.
        zDexecutable_path has been deprecated, please pass in a Service object   )
stacklevelz[capabilities and desired_capabilities have been deprecated, please pass in a Service objectzCfirefox_binary has been deprecated, please pass in a Service objectNzEfirefox_profile has been deprecated, please pass in an Options objectz=log_path has been deprecated, please pass in a Service objectzEservice_log_path has been deprecated, please pass in a Service objectzAservice_args has been deprecated, please pass in a Service objectbinaryZacceptInsecureCertsF)service_argslog_path)Zremote_server_addrZignore_proxyT)Zcommand_executoroptions
keep_alive)r   warningswarnDeprecationWarningr   profileDEFAULT_LOG_PATHDEFAULT_SERVICE_LOG_PATHservicer   ZFIREFOXcopyr
   dictget
isinstancestrr   r	   Zaccept_insecure_certsr   startr   Zservice_urlZ_ignore_local_proxysuper__init__Z
_is_remote)selffirefox_profilefirefox_binaryZcapabilitiesproxyZexecutable_pathr   Zservice_log_pathr   r   Zdesired_capabilitiesr   r   executor	__class__ O/usr/local/lib/python3.10/dist-packages/selenium/webdriver/firefox/webdriver.pyr$   -   s   7






zWebDriver.__init__returnc              
      s   zt    W n	 ty   Y nw | j  | jrIzt| jj | jjr-t| jj W dS W dS  tyH } zt	t
| W Y d}~dS d}~ww dS )z3Quits the driver and close every associated window.N)r#   quit	Exceptionr   stopr   r   pathZ
tempfolderprintr!   )r%   er*   r,   r-   r/      s"   
zWebDriver.quitc                 C   s   | j S )N)r   r%   r,   r,   r-   r&      s   zWebDriver.firefox_profilec                 C   s   |  dd|i d S )NZSET_CONTEXTcontextexecute)r%   r6   r,   r,   r-   set_context   s   zWebDriver.set_contextc              	   c   s@    |  dd}| | zdV  W | | dS | | w )a  Sets the context that Selenium commands are running in using
        a `with` statement. The state of the context on the server is
        saved before entering the block, and restored upon exiting it.

        :param context: Context, may be one of the class properties
            `CONTEXT_CHROME` or `CONTEXT_CONTENT`.

        Usage example::

            with selenium.context(selenium.CONTEXT_CHROME):
                # chrome scope
                ... do stuff ...
        ZGET_CONTEXTvalueN)r8   popr9   )r%   r6   Zinitial_contextr,   r,   r-   r6      s   
zWebDriver.contextFc              	   C   s   t j|rSt }t|d }t|dtj)}t |D ]\}}}|D ]}	t j	||	}
|
|
|
|d  q$qW d   n1 sCw   Y  t| d}nt|d}t| d}W d   n1 smw   Y  ||d}| d|d S )	a>  
        Installs Firefox addon.

        Returns identifier of installed addon. This identifier can later
        be used to uninstall addon.

        :param path: Absolute path to the addon that will be installed.

        :Usage:
            ::

                driver.install_addon('/path/to/firebug.xpi')
        r   wNzUTF-8rb)addon	temporaryZINSTALL_ADDONr:   )osr2   isdirr   lenzipfileZipFileZIP_DEFLATEDwalkjoinwritebase64	b64encodegetvaluedecodeopenreadr8   )r%   r2   r?   fpZ	path_rootZzippedbasedirsfilesZfylefilenamer>   filepayloadr,   r,   r-   install_addon   s"   
zWebDriver.install_addonc                 C   s   |  dd|i dS )z
        Uninstalls Firefox addon using its identifier.

        :Usage:
            ::

                driver.uninstall_addon('addon@foo.com')
        ZUNINSTALL_ADDONidNr7   )r%   
identifierr,   r,   r-   uninstall_addon     	zWebDriver.uninstall_addonc                 C   s   |  dstdt |  }z1z$t|d}|| W d   n1 s(w   Y  W W ~dS W W ~dS  tyB   Y W ~dS w ~w )a  
        Saves a full document screenshot of the current window to a PNG image file. Returns
           False if there is any IOError, else returns True. Use full paths in
           your filename.

        :Args:
         - filename: The full path you wish to save your screenshot to. This
           should end with a `.png` extension.

        :Usage:
            ::

                driver.get_full_page_screenshot_as_file('/Screenshots/foo.png')
        z.pngz^name used for saved screenshot does not match file type. It should end with a `.png` extensionwbNFT)	lowerendswithr   r   UserWarningget_full_page_screenshot_as_pngrM   rH   OSError)r%   rS   Zpngfr,   r,   r-    get_full_page_screenshot_as_file  s&    z*WebDriver.get_full_page_screenshot_as_filec                 C   s
   |  |S )a  
        Saves a full document screenshot of the current window to a PNG image file. Returns
           False if there is any IOError, else returns True. Use full paths in
           your filename.

        :Args:
         - filename: The full path you wish to save your screenshot to. This
           should end with a `.png` extension.

        :Usage:
            ::

                driver.save_full_page_screenshot('/Screenshots/foo.png')
        )rb   )r%   rS   r,   r,   r-   save_full_page_screenshot/  s   
z#WebDriver.save_full_page_screenshotc                 C   s   t |  dS )z
        Gets the full document screenshot of the current window as a binary data.

        :Usage:
            ::

                driver.get_full_page_screenshot_as_png()
        ascii)rI   	b64decode"get_full_page_screenshot_as_base64encoder5   r,   r,   r-   r_   @  rZ   z)WebDriver.get_full_page_screenshot_as_pngc                 C   s   |  dd S )z
        Gets the full document screenshot of the current window as a base64 encoded string
           which is useful in embedded images in HTML.

        :Usage:
            ::

                driver.get_full_page_screenshot_as_base64()
        ZFULL_PAGE_SCREENSHOTr:   r7   r5   r,   r,   r-   rf   K  s   
z,WebDriver.get_full_page_screenshot_as_base64)r.   N)F)__name__
__module____qualname__ZCONTEXT_CHROMEZCONTEXT_CONTENTr   r   r   r$   r/   propertyr&   r9   r   r6   r!   rV   rY   boolrb   rc   r_   rf   __classcell__r,   r,   r*   r-   r   (   s.     



r   )rI   ior   r@   shutilr   r   
contextlibr   rC   Z.selenium.webdriver.common.desired_capabilitiesr   Z#selenium.webdriver.remote.webdriverr   ZRemoteWebDriverr'   r   r&   r	   r   r
   Zremote_connectionr   r   r   r   r   r   r,   r,   r,   r-   <module>   s"   