B
     ¸h)$  ã               @   s0   d dl mZ d dlmZmZ G dd„ deƒZdS )é    )ÚBy)ÚNoSuchElementExceptionÚUnexpectedTagNameExceptionc               @   s   e Zd Z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„ Zdd„ Zdd„ Zdd„ ZdS ) ÚSelectc             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 - element 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)Útag_nameÚlowerr   Ú_elÚget_attributeÚis_multiple)ÚselfÚ
webelementZmulti© r   ú^C:\Users\sanjo\AppData\Local\Qlobot\Launcher\ext_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   ÚTAG_NAME)r   r   r   r   Úoptions,   s    zSelect.optionsc             C   s*   g }x | j D ]}| ¡ r| |¡ qW |S )zCReturns a list of all selected options belonging to this select tag)r   Úis_selectedÚappend)r   ÚretÚoptr   r   r   Úall_selected_options1   s
    zSelect.all_selected_optionsc             C   s(   x| j D ]}| ¡ r|S qW 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_option:   s    zSelect.first_selected_optionc             C   s\   d|   |¡ }| j tj|¡}d}x$|D ]}|  |¡ | js@dS d}q(W |sXt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 specisied value in SELECT
           zoption[value =%s]FNTz#Cannot locate option with value: %s)Ú_escapeStringr
   r   r   ÚCSS_SELECTORÚ_setSelectedr   r   )r   ÚvalueÚcssÚoptsÚmatchedr   r   r   r   Úselect_by_valueC   s    

zSelect.select_by_valuec             C   sD   t |ƒ}x*| jD ] }| d¡|kr|  |¡ dS qW td| ƒ‚dS )aG  Select the option at the given index. This is done by examing 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 specisied index in SELECT
           ÚindexNz&Could not locate element with index %d)Ústrr   r   r   r   )r   r$   Úmatchr   r   r   r   Úselect_by_indexY   s    	
zSelect.select_by_indexc       	      C   sØ   d|   |¡ }| j tj|¡}d}x$|D ]}|  |¡ | js@dS d}q(W t|ƒdkrÄd|krÄ|  |¡}|dkrv| j	}nd|   |¡ }| j tj|¡}x.|D ]&}||j
krš|  |¡ | js¼dS d}qšW |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 specisied text in SELECT
           z".//option[normalize-space(.) = %s]FNTr   ú Ú z.//option[contains(.,%s)]z.Could not locate element with visible text: %s)r   r
   r   r   ÚXPATHr   r   ÚlenÚ_get_longest_tokenr   Útextr   )	r   r-   Úxpathr!   r"   r   ZsubStringWithoutSpaceÚ
candidatesÚ	candidater   r   r   Úselect_by_visible_texti   s,    





zSelect.select_by_visible_textc             C   s,   | j stdƒ‚x| jD ]}|  |¡ qW 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   Ú_unsetSelected)r   r   r   r   r   Údeselect_allŽ   s    zSelect.deselect_allc             C   s`   | j stdƒ‚d}d|  |¡ }| j tj|¡}x|D ]}|  |¡ d}q6W |s\t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 specisied value in SELECT
        z/You may only deselect options of a multi-selectFzoption[value = %s]Tz'Could not locate element with value: %sN)	r   r2   r   r
   r   r   r   r3   r   )r   r   r"   r    r!   r   r   r   r   Údeselect_by_value—   s    

zSelect.deselect_by_valuec             C   sN   | j stdƒ‚x.| jD ]$}| d¡t|ƒkr|  |¡ dS qW td| ƒ‚dS )aI  Deselect the option at the given index. This is done by examing 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 specisied index in SELECT
        z/You may only deselect options of a multi-selectr$   Nz&Could not locate element with index %d)r   r2   r   r   r%   r3   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|¡}x|D ]}|  |¡ d}q6W |s\t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
        z/You may only deselect options of a multi-selectFz".//option[normalize-space(.) = %s]Tz.Could not locate element with visible text: %sN)	r   r2   r   r
   r   r   r*   r3   r   )r   r-   r"   r.   r!   r   r   r   r   Údeselect_by_visible_text¾   s    	

zSelect.deselect_by_visible_textc             C   s   |  ¡ s| ¡  d S )N)r   Úclick)r   r   r   r   r   r   Ò   s    zSelect._setSelectedc             C   s   |  ¡ r| ¡  d S )N)r   r8   )r   r   r   r   r   r3   Ö   s    zSelect._unsetSelectedc             C   sŒ   d|krtd|krt|  d¡}dg}x$|D ]}| d| ¡ | d¡ q&W |dd… }| d¡rf| d¡ d	 |¡d
 S d|kr„d| S d| S )Nú"ú'zconcat(z"%s"z, '"', r   éÿÿÿÿz, '"'r)   ú)z'%s')Úsplitr   ÚendswithÚjoin)r   r   Z
substringsÚresultZ	substringr   r   r   r   Ú   s    



zSelect._escapeStringc             C   s4   |  d¡}d}x |D ]}t|ƒt|ƒkr|}qW |S )Nr(   r)   )r=   r+   )r   r   ÚitemsZlongestÚitemr   r   r   r,   ë   s    

zSelect._get_longest_tokenN)Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   r   r   r#   r'   r1   r4   r5   r6   r7   r   r3   r   r,   r   r   r   r   r      s   		%	r   N)Úselenium.webdriver.common.byr   Úselenium.common.exceptionsr   r   Úobjectr   r   r   r   r   Ú<module>   s   