a
    5ñ»j’  ã                   @   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	 d dl
mZ ddlmZ ddlmZ dd	lmZ G d
d„ deƒZdS )é    N)Úcontextmanager)ÚBytesIO)ÚDriverFinder)Ú	WebDriveré   )ÚOptions)ÚFirefoxRemoteConnection)ÚServicec                       s¸   e Zd ZdZdZdZdeeeddœ‡ fdd„Z	dd	œ‡ fd
d„Z
dd	œdd„Zedd„ ƒZded	œdd„Zdd	œdd„Zed	œdd„Zed	œdd„Zed	œdd„Zed	œdd„Z‡  ZS )r   z=Controls the GeckoDriver and allows you to drive the browser.ÚchromeÚcontentNT)ÚoptionsÚserviceÚ
keep_aliveÚreturnc                    s²   |r|nt ƒ | _|r|ntƒ }t| j|ƒ}| ¡ rB| ¡ |_d|_| j ¡ pR| ¡ | j_	| j 
¡  t| jj||jd�}ztƒ j||d� W n ty¦   |  ¡  ‚ Y n0 d| _dS )a—  Creates a new instance of the Firefox driver. Starts the service and
        then creates new instance of Firefox driver.

        :Args:
         - options - Instance of ``options.Options``.
         - service - (Optional) service instance for managing the starting and stopping of the driver.
         - keep_alive - Whether to configure remote_connection.RemoteConnection to use HTTP keep-alive.
        N)Zremote_server_addrr   Zignore_proxy)Zcommand_executorr   F)r	   r   r   r   Zget_browser_pathZbinary_locationZbrowser_versionZenv_pathZget_driver_pathÚpathÚstartr   Zservice_urlZ_ignore_local_proxyÚsuperÚ__init__Ú	ExceptionÚquitZ
_is_remote)Úselfr   r   r   ÚfinderÚexecutor©Ú	__class__© úJE:\soccer\ml_env\Lib\site-packages\selenium/webdriver/firefox/webdriver.pyr   &   s&    

ýzWebDriver.__init__)r   c                    sB   z0zt ƒ  ¡  W n ty"   Y n0 W | j ¡  n| j ¡  0 dS )z=Closes the browser and shuts down the GeckoDriver executable.N)r   r   r   r   Ústop©r   r   r   r   r   N   s
    zWebDriver.quitc                 C   s   |   dd|i¡ d S )NZSET_CONTEXTÚcontext©Úexecute)r   r   r   r   r   Úset_contextX   s    zWebDriver.set_contextc              	   c   s@   |   d¡ d¡}|  |¡ zdV  W |  |¡ n|  |¡ 0 dS )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)r!   Úpopr"   )r   r   Zinitial_contextr   r   r   r   [   s
    
zWebDriver.contextFc              	   C   s  t j |¡r¶tƒ }t j |¡}t|ƒd }tj|dtjdd��R}t  	|¡D ]6\}}}|D ]&}	t j 
||	¡}
| |
|
|d… ¡ qXqJW d  ƒ n1 s–0    Y  t | ¡ ¡ d¡}n>t|dƒ�$}t | ¡ ¡ d¡}W d  ƒ n1 sê0    Y  ||dœ}|  d	|¡d
 S )a’  Installs Firefox addon.

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

        :param temporary: allows you to load browser extensions temporarily during a session
        :param path: Absolute path to the addon that will be installed.

        :Usage:
            ::

                driver.install_addon('/path/to/firebug.xpi')
        r   ÚwF)Ústrict_timestampsNzUTF-8Úrb)ÚaddonÚ	temporaryZINSTALL_ADDONr#   )Úosr   Úisdirr   ÚnormpathÚlenÚzipfileÚZipFileÚZIP_DEFLATEDÚwalkÚjoinÚwriteÚbase64Ú	b64encodeÚgetvalueÚdecodeÚopenÚreadr!   )r   r   r)   ÚfpZ	path_rootZzippedÚbaseÚ_ÚfilesZfyleÚfilenamer(   ÚfileÚpayloadr   r   r   Úinstall_addonq   s    62
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ÚidNr    )r   Ú
identifierr   r   r   Úuninstall_addon“   s    zWebDriver.uninstall_addonc                 C   s„   |  ¡  d¡st dt¡ |  ¡ }zXz8t|dƒ�}| |¡ W d  ƒ n1 sP0    Y  W n tyt   Y W ~dS 0 W ~n~0 d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.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ÚendswithÚwarningsÚwarnÚUserWarningÚget_full_page_screenshot_as_pngr8   r3   ÚOSError)r   r>   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')
        )rN   )r   r>   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)r4   Ú	b64decodeÚ"get_full_page_screenshot_as_base64Úencoder   r   r   r   rK   Ê   s    	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#   r    r   r   r   r   rR   Õ   s    	z,WebDriver.get_full_page_screenshot_as_base64)NNT)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZCONTEXT_CHROMEZCONTEXT_CONTENTr   r	   Úboolr   r   r"   r   r   ÚstrrA   rD   rN   rO   ÚbytesrK   rR   Ú__classcell__r   r   r   r   r       s,      üû(

"
r   )r4   r*   rH   r.   Ú
contextlibr   Úior   Z'selenium.webdriver.common.driver_finderr   Z#selenium.webdriver.remote.webdriverr   ZRemoteWebDriverr   r   Zremote_connectionr   r   r	   r   r   r   r   Ú<module>   s   