zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
U
\±d<$ã@s6ddlmZddlmZddlmZGdd„dƒZdS)é)ÚNoSuchElementException)ÚUnexpectedTagNameException)ÚByc@s¸eZdZddœdd„Zedd„ƒZedd„ƒZed    d
„ƒZd d „Zd d„Z    dd„Z
ddœdd„Z dd„Z dd„Z dd„Zddœdd„Zddœdd„Zeedœdd„Zeedœd d!„ZdS)"ÚSelectN)ÚreturncCsB|j ¡dkrtd|j›ƒ‚||_|j d¡}|o:|dk|_dS)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)
        Úselectz/Select only works on <select> elements, not on ÚmultipleÚfalseN)Ztag_nameÚlowerrÚ_elZget_dom_attributeÚ is_multiple)ÚselfZ
webelementZmulti©rúXd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\selenium/webdriver/support/select.pyÚ__init__s
 zSelect.__init__cCs|j tjd¡S)z;Returns a list of all options belonging to this select tag.Úoption)r Ú find_elementsrZTAG_NAME©r rrrÚoptions)szSelect.optionscCsdd„|jDƒS)zLReturns a list of all selected options belonging to this select
        tag.cSsg|]}| ¡r|‘qSr)Ú is_selected)Ú.0ÚoptrrrÚ
<listcomp>2sz/Select.all_selected_options.<locals>.<listcomp>)rrrrrÚall_selected_options.szSelect.all_selected_optionscCs(|jD]}| ¡r|Sqtdƒ‚dS)zjThe first selected option in this select tag (or the currently
        selected option in a normal select)zNo options are selectedN)rrr©r rrrrÚfirst_selected_option4s
 
zSelect.first_selected_optioncCs`d| |¡›d}|j tj|¡}d}|D]}| |¡|jsDdSd}q*|s\td|›ƒ‚dS)aLSelect 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
        zoption[value =ú]FNTz!Cannot locate option with value: )Ú_escape_stringr rrÚ CSS_SELECTORÚ _set_selectedr r)r ÚvalueÚcssÚoptsÚmatchedrrrrÚselect_by_value=s 
zSelect.select_by_valuecCsDt|ƒ}|jD]"}| d¡|kr| |¡dSqtd|›ƒ‚dS)a: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 )ÚstrrÚ get_attributerr)r r%ÚmatchrrrrÚselect_by_indexSs     
 
zSelect.select_by_indexc    CsÞd| |¡›d}|j tj|¡}d}|D]}| |¡|jsDdSd}q*t|ƒdkrÈd|krÈ| |¡}|dkrx|j    }n"d    | |¡›d
}|j tj|¡}|D](}||j
krž| |¡|jsÂdSd}qž|sÚt d |›ƒ‚dS) aSSelect 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(.) = rFNTrú Úz.//option[contains(.,z)]ú,Could not locate element with visible text: ) rr rrÚXPATHrr ÚlenÚ_get_longest_tokenrÚtextr)    r r2Úxpathr"r#rZsub_string_without_spaceÚ
candidatesÚ    candidaterrrÚselect_by_visible_textcs, 
 
 
 
zSelect.select_by_visible_textcCs(|jstdƒ‚|jD]}| |¡qdS)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_selectedrrrrÚ deselect_allˆs
zSelect.deselect_allcCsb|jstdƒ‚d}d| |¡›d}|j tj|¡}|D]}| |¡d}q8|s^td|›ƒ‚dS)aRDeselect 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-selectFzoption[value = rTz%Could not locate element with value: N)    r r7rr rrrr8r)r r r#r!r"rrrrÚdeselect_by_value”s 
zSelect.deselect_by_valuecCsN|jstdƒ‚|jD]&}| d¡t|ƒkr| |¡dSqtd|›ƒ‚dS)a?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 r7rr(r'r8r)r r%rrrrÚdeselect_by_indexªs    
 
zSelect.deselect_by_indexcCsb|jstdƒ‚d}d| |¡›d}|j tj|¡}|D]}| |¡d}q8|s^td|›ƒ‚dS)zú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+rTr.N)    r r7rr rrr/r8r)r r2r#r3r"rrrrÚdeselect_by_visible_text»s    
zSelect.deselect_by_visible_textcCs$| ¡s | ¡stdƒ‚| ¡dS)Nz$You may not select a disabled option)rÚ
is_enabledr7Úclick©r rrrrrÏszSelect._set_selectedcCs| ¡r| ¡dS)N)rr?r@rrrr8ÕszSelect._unset_selected)r rcCs”d|krtd|krt| d¡}dg}|D] }| d|›d¡| d¡q$|dd…}| d¡rf| d¡d |¡d    Sd|krˆd|›dSd|›dS)
Nú"ú'zconcat(z, '"', réÿÿÿÿz, '"'r-ú))ÚsplitÚappendÚendswithÚjoin)r r Z
substringsÚresultZ    substringrrrrÙs
 
 
 zSelect._escape_stringcCs0| d¡}d}|D]}t|ƒt|ƒkr|}q|S)Nr,r-)rEr0)r r ÚitemsÚlongestÚitemrrrr1ês 
zSelect._get_longest_token)Ú__name__Ú
__module__Ú __qualname__rÚpropertyrrrr$r*r6r9r;r<r=rr8r'rr1rrrrrs$
 
 
% rN)Zselenium.common.exceptionsrrZselenium.webdriver.common.byrrrrrrÚ<module>s