a
    ¾Ê.c&$  ã                   @   s.   d dl mZ d dlmZmZ G dd„ dƒZdS )é    )ÚBy)ÚNoSuchElementExceptionÚUnexpectedTagNameExceptionc                   @   s²   e Zd Zddœdd„Zedd„ ƒZedd„ ƒZed	d
„ ƒZdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zddœdd„Zddœdd„Zeedœdd„Zeedœd d!„ZdS )"ÚSelectN)Úreturnc                 C   s@   |j  ¡ dkrtd|j  ƒ‚|| _| j d¡}|o8|dk| _dS )a…  
        Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not,
        then an UnexpectedTagNameException is thrown.

        :Args:
         - webelement - SELECT element to wrap

        Example:
            from selenium.webdriver.support.ui import Select 

            Select(driver.find_element(By.TAG_NAME, "select")).select_by_index(2)
        Úselectz3Select only works on <select> elements, not on <%s>ZmultipleÚfalseN)Ztag_nameÚlowerr   Ú_elZget_dom_attributeÚis_multiple)ÚselfZ
webelementÚmulti© r   ú[/var/www/brookimports/venv/lib/python3.9/site-packages/selenium/webdriver/support/select.pyÚ__init__   s    ÿÿzSelect.__init__c                 C   s   | j  tjd¡S )z:Returns a list of all options belonging to this select tagÚoption)r
   Úfind_elementsr   ZTAG_NAME©r   r   r   r   Úoptions,   s    zSelect.optionsc                 C   s   dd„ | j D ƒS )zCReturns a list of all selected options belonging to this select tagc                 S   s   g | ]}|  ¡ r|‘qS r   )Úis_selected)Ú.0Úoptr   r   r   Ú
<listcomp>4   ó    z/Select.all_selected_options.<locals>.<listcomp>)r   r   r   r   r   Úall_selected_options1   s    zSelect.all_selected_optionsc                 C   s(   | j D ]}| ¡ r|  S qtdƒ‚dS )zjThe first selected option in this select tag (or the currently selected option in a
        normal select)zNo options are selectedN)r   r   r   ©r   r   r   r   r   Úfirst_selected_option6   s    

zSelect.first_selected_optionc                 C   sZ   d|   |¡ }| j tj|¡}d}|D ]}|  |¡ | js@ dS d}q&|sVtd| ƒ‚dS )a^  Select all options that have a value matching the argument. That is, when given "foo" this
           would select an option like:

           <option value="foo">Bar</option>

           :Args:
            - value - The value to match against

           throws NoSuchElementException If there is no option with specified value in SELECT
           zoption[value =%s]FNTz#Cannot locate option with value: %s)Ú_escape_stringr
   r   r   ÚCSS_SELECTORÚ_set_selectedr   r   )r   ÚvalueÚcssÚoptsÚmatchedr   r   r   r   Úselect_by_value?   s    
zSelect.select_by_valuec                 C   sB   t |ƒ}| jD ]"}| d¡|kr|  |¡  dS qtd| ƒ‚dS )aI  Select the option at the given index. This is done by examining the "index" attribute of an
           element, and not merely by counting.

           :Args:
            - index - The option at this index will be selected

           throws NoSuchElementException If there is no option with specified index in SELECT
           ÚindexNú&Could not locate element with index %d)Ústrr   Úget_attributer   r   )r   r%   Úmatchr   r   r   r   Úselect_by_indexU   s    	

zSelect.select_by_indexc           	      C   sÔ   d|   |¡ }| j tj|¡}d}|D ]}|  |¡ | js@ dS d}q&t|ƒdkrÀd|v rÀ|  |¡}|dkrt| j	}nd|   |¡ }| j tj|¡}|D ](}||j
kr–|  |¡ | jsº dS d}q–|sÐtd	| ƒ‚dS )
ae  Select all options that display text matching the argument. That is, when given "Bar" this
           would select an option like:

            <option value="foo">Bar</option>

           :Args:
            - text - The visible text to match against

            throws NoSuchElementException If there is no option with specified text in SELECT
           ú".//option[normalize-space(.) = %s]FNTr   ú Ú z.//option[contains(.,%s)]ú.Could not locate element with visible text: %s)r   r
   r   r   ÚXPATHr   r   ÚlenÚ_get_longest_tokenr   Útextr   )	r   r2   Úxpathr"   r#   r   Zsub_string_without_spaceÚ
candidatesÚ	candidater   r   r   Úselect_by_visible_texte   s,    



zSelect.select_by_visible_textc                 C   s(   | j stdƒ‚| jD ]}|  |¡ qdS )z¾Clear all selected entries. This is only valid when the SELECT supports multiple selections.
           throws NotImplementedError If the SELECT does not support multiple selections
        z3You may only deselect all options of a multi-selectN)r   ÚNotImplementedErrorr   Ú_unset_selectedr   r   r   r   Údeselect_allŠ   s    
zSelect.deselect_allc                 C   s\   | j stdƒ‚d}d|  |¡ }| j tj|¡}|D ]}|  |¡ d}q4|sXtd| ƒ‚dS )aa  Deselect all options that have a value matching the argument. That is, when given "foo" this
           would deselect an option like:

            <option value="foo">Bar</option>

           :Args:
            - value - The value to match against

            throws NoSuchElementException If there is no option with specified value in SELECT
        ú/You may only deselect options of a multi-selectFzoption[value = %s]Tz'Could not locate element with value: %sN)	r   r7   r   r
   r   r   r   r8   r   )r   r    r#   r!   r"   r   r   r   r   Údeselect_by_value“   s    
zSelect.deselect_by_valuec                 C   sL   | j stdƒ‚| jD ]&}| d¡t|ƒkr|  |¡  dS qtd| ƒ‚dS )aK  Deselect the option at the given index. This is done by examining the "index" attribute of an
           element, and not merely by counting.

           :Args:
            - index - The option at this index will be deselected

            throws NoSuchElementException If there is no option with specified index in SELECT
        r:   r%   Nr&   )r   r7   r   r(   r'   r8   r   )r   r%   r   r   r   r   Údeselect_by_index©   s    	

zSelect.deselect_by_indexc                 C   s\   | j stdƒ‚d}d|  |¡ }| j tj|¡}|D ]}|  |¡ d}q4|sXtd| ƒ‚dS )a  Deselect all options that display text matching the argument. That is, when given "Bar" this
           would deselect an option like:

           <option value="foo">Bar</option>

           :Args:
            - text - The visible text to match against
        r:   Fr+   Tr.   N)	r   r7   r   r
   r   r   r/   r8   r   )r   r2   r#   r3   r"   r   r   r   r   Údeselect_by_visible_textº   s    	
zSelect.deselect_by_visible_textc                 C   s   |  ¡ s| ¡  d S ©N©r   Zclick©r   r   r   r   r   r   Î   s    zSelect._set_selectedc                 C   s   |  ¡ r| ¡  d S r>   r?   r@   r   r   r   r8   Ò   s    zSelect._unset_selected)r    r   c                 C   sˆ   d|v rpd|v rp|  d¡}dg}|D ]}| d| ¡ | d¡ q$|dd… }| d¡rb| d¡ d	 |¡d
 S d|v r€d| S d| S )Nú"ú'zconcat(z"%s"z, '"', r   éÿÿÿÿz, '"'r-   ú)z'%s')ÚsplitÚappendÚendswithÚjoin)r   r    Z
substringsÚresultZ	substringr   r   r   r   Ö   s    


zSelect._escape_stringc                 C   s0   |  d¡}d}|D ]}t|ƒt|ƒkr|}q|S )Nr,   r-   )rE   r0   )r   r    ÚitemsÚlongestÚitemr   r   r   r1   ç   s    
zSelect._get_longest_token)Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   r   r   r$   r*   r6   r9   r;   r<   r=   r   r8   r'   r   r1   r   r   r   r   r      s$   


%	r   N)Zselenium.webdriver.common.byr   Zselenium.common.exceptionsr   r   r   r   r   r   r   Ú<module>   s   