o
    ѷ6ib                     @   sH   d dl Z d dl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)BinaryIO)DesiredCapabilities)
ArgOptionsc                       s6  e Zd ZdZd  fddZedefddZejdeddfd	dZededB fd
dZ	e	jdeddfddZ	ede
e fddZdeddfddZdeddfddZedefddZdedeeB eB e
e B ddfddZedefddZejdeddfddZdefddZedefddZ  ZS )!ChromiumOptionszgoog:chromeOptionsreturnNc                    s2   t    d| _g | _g | _i | _d| _d| _dS )z1Initialize ChromiumOptions with default settings. NF)super__init___binary_location_extension_files_extensions_experimental_options_debugger_address_enable_webextensionsself	__class__ h/var/www/hoanhtaovolam_webdjango/env/lib/python3.10/site-packages/selenium/webdriver/chromium/options.pyr	      s   

zChromiumOptions.__init__c                 C      | j S )z>Returns the location of the binary, otherwise an empty string.)r
   r   r   r   r   binary_location'      zChromiumOptions.binary_locationvaluec                 C   s   t |ts
t| j|| _dS )zzAllows you to set where the chromium binary lives.

        Args:
            value: Path to the Chromium binary.
        N)
isinstancestr	TypeErrorBINARY_LOCATION_ERRORr
   r   r   r   r   r   r   ,   s   


c                 C   r   )z4Returns the address of the remote devtools instance.)r   r   r   r   r   debugger_address7   r   z ChromiumOptions.debugger_addressc                 C   s   t |ts	td|| _dS )zSet the address of the remote devtools instance for active wait connection.

        Args:
            value: Address of remote devtools instance if any (hostname[:port]).
        z!Debugger Address must be a stringN)r   r   r   r   r   r   r   r   r   <   s   

c              	   C   sd   dt dtfdd}g }| jD ]}t|d}||| W d   n1 s'w   Y  q|| j S )z9Returns a list of encoded extensions that will be loaded.	file_datar   c                 S   s   t |  dS )Nzutf-8)base64	b64encodereaddecode)r    r   r   r   _decodeK   s   z+ChromiumOptions.extensions.<locals>._decoderbN)r   r   r   openappendr   )r   r%   encoded_extensions	extensionfr   r   r   
extensionsG   s   

zChromiumOptions.extensionsr*   c                 C   sD   |rt jt j|}t j|r| j| dS tdtd)zAdd the path to an extension to be extracted to ChromeDriver.

        Args:
            extension: Path to the *.crx file.
        z#Path to the extension doesn't existargument can not be nullN)	ospathabspath
expanduserexistsr   r(   OSError
ValueError)r   r*   extension_to_addr   r   r   add_extensionX   s   zChromiumOptions.add_extensionc                 C   s   |r
| j | dS td)zAdd Base64-encoded string with extension data to be extracted to ChromeDriver.

        Args:
            extension: Base64 encoded string with extension data.
        r-   N)r   r(   r4   )r   r*   r   r   r   add_encoded_extensiong   s   z%ChromiumOptions.add_encoded_extensionc                 C   r   )z:Returns a dictionary of experimental options for chromium.r   r   r   r   r   experimental_optionsr   r   z$ChromiumOptions.experimental_optionsnamec                 C   s   || j |< dS )zAdds an experimental option which is passed to chromium.

        Args:
            name: The experimental option name.
            value: The option value.
        Nr8   )r   r:   r   r   r   r   add_experimental_optionw   s   z'ChromiumOptions.add_experimental_optionc                 C   r   )zKReturn whether webextension support is enabled for Chromium-based browsers.)r   r   r   r   r   enable_webextensions   r   z$ChromiumOptions.enable_webextensionsc                 C   s`   || _ |rddg}|D ]}|| jvr| | qdS ddg}|D ]}|| jv r-| j| q dS )a  Enables or disables webextension support for Chromium-based browsers.

        Args:
            value: True to enable webextension support, False to disable.

        Notes:
            - When enabled, this automatically adds the required Chromium flags:
                - --enable-unsafe-extension-debugging
                - --remote-debugging-pipe
            - When disabled, this removes BOTH flags listed above, even if they were manually added via add_argument()
              before enabling webextensions.
            - Enabling --remote-debugging-pipe makes the connection b/w chromedriver
              and the browser use a pipe instead of a port, disabling many CDP functionalities
              like devtools
        z#--enable-unsafe-extension-debuggingz--remote-debugging-pipeN)r   
_argumentsadd_argumentremove)r   r   required_flagsflagflags_to_remover   r   r   r<      s   


c                 C   sd   | j }| j }| jr|| j | j|d< | jr| j|d< | j|d< | jr+| j|d< ||| j	< |S )zCreates a capabilities with all the options that have been set.

        Returns:
            A dictionary with all set options.
        r,   binaryargsdebuggerAddress)
_capsr9   copymobile_optionsupdater,   r   r=   r   KEY)r   capschrome_optionsr   r   r   to_capabilities   s   





zChromiumOptions.to_capabilitiesc                 C   s
   t j S )N)r   CHROMErG   r   r   r   r   default_capabilities   s   
z$ChromiumOptions.default_capabilities)r   N)__name__
__module____qualname__rJ   r	   propertyr   r   setterr   listr,   r6   r7   dictr9   intr;   boolr<   rM   rO   __classcell__r   r   r   r   r      s2    


&	r   )	r!   r.   typingr   .selenium.webdriver.common.desired_capabilitiesr   !selenium.webdriver.common.optionsr   r   r   r   r   r   <module>   s   