Selenium series (X) - Operation and interpretation for Select Source drop-down box

If you want to learn from scratch Selenium, you can look at this series of articles Oh!

https://www.cnblogs.com/poloyy/category/1680176.html

 

Secondly, if you do not understand the basics of the front, so you need to replenish Oh, bloggers temporarily not learned (though I would, so I do not have to learn selenium review the front ha ha ha ...)

 

First, save the following html code into a file

Follow-up code to access this small cases are the html <! DOCTYPE html >

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>下拉框</title>
</head>
<body>

<select id="pro"> <option value="gd">广东</option> <option value="hb">湖北</option> <option value="gj">北京</option> </select>

<select id="city" multiple> <option value="gz">广州</option> <option value="wh">武汉</option> <option value="gj">北京</option> </select> </body> </html>

note

If the select drop-down box has  multiple  attributes, you can multi-select option, but this is not common

 

Operating on the drop-down box

  • Back to all options
  • Return all selected options
  • Uncheck the option selected by the value attribute or
  • Uncheck the option selected by the index or index
  • Uncheck the option to select the text or by inter-label
  • Uncheck all options

 

Options & select the operating return

# ! / Usr / bin / env Python 
# - * - Coding: UTF-8 - * - 

"" " 
__title__ = 
__TIME__ = 2020/3/25 17:52 
__Author__ = small pineapple test notes 
__Blog__ = https: // www. cnblogs.com/poloyy/ 
"" " 
from Time Import SLEEP 

from selenium.webdriver.support.select Import the Select
 from the Selenium Import webdriver 

Driver = webdriver.Chrome ( " ../resources/chromedriver.exe " ) 

# will change the html file own path 
driver.get ( " File: /// C: / drop-down box .html " ) 
driver.maximize_window ()

# Find select tag element 
Pro = the Select (driver.find_element_by_id ( " Pro " )) 

# returns all options 
for the Option in pro.options:
     Print (option.text) 

# returns all selected options 
for the Option in pro.all_selected_options:
     Print (option.text) 

# by value selected 
pro.select_by_value ( " BJ " ) 
SLEEP ( 1 ) 

# by index selected 
pro.select_by_index (1 ) 
SLEEP ( 1 ) 

#Selected by the label text 
pro.select_by_visible_text ( " Guangdong " )

 

Uncheck operation

# Find id = city drop-down box 
City = the Select (driver.find_element_by_id ( " City " )) 

# Select 
for the Option in city.options:
     IF  not option.is_selected (): 
        city.select_by_visible_text (option.text) 
SLEEP ( 1 ) 

# according to value uncheck 
city.deselect_by_value ( " BJ " ) 
SLEEP ( 1 ) 

# according to index uncheck 
city.deselect_by_index (0) 
SLEEP ( 1 ) 

# selected according to the label text 
city.deselect_by_visible_text ( " Wuhan" ) 
SLEEP ( 1 ) 

# Select 
for the Option in city.options:
     IF  not option.is_selected (): 
        city.select_by_visible_text (option.text) 
SLEEP ( 1 ) 

# uncheck all options 
city.deselect_all ()

Knowledge Point

Cancel the operation applies only to add multiple drop-down box, otherwise it will error

    raise NotImplementedError("You may only deselect options of a multi-select")
NotImplementedError: You may only deselect options of a multi-select

 

Select Source Reading

class Select(object):

    def __init__(self, webelement):
        """
        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 \n
            Select(driver.find_element_by_tag_name("select")).select_by_index(2)
        """

Knowledge Point

  • Examples of  Select  need to pass  select  the drop-down box  webelement 
  • If the incoming  webelement  of  tag_name  not  <select> .. </ select>  tag, an exception is thrown  UnexpectedTagNameException 

 

Guess you like

Origin www.cnblogs.com/poloyy/p/12601101.html