Selenium3 Python WebDriver API source code analysis (17) drop-down box (select) support

Overview

SeleniumA lot of code is needed in the process of implementing the automatic operation of the drop-down box (select), so Seleniuman API is provided specifically for the drop-down box (select), that is selenium\webdriver\support\select.py, the Selectclass in the module .

SelectThe main elements of the class are as follows:

  • __init__(self, webelement): Construction method to check whether the element is a drop-down box (select), and whether the drop-down box supports multiple selections.
  • optionsFeatures: Return to the drop-down box option list.
  • all_selected_optionsFeatures: Return to the list of selected options in the drop-down box.
  • first_selected_optionFeatures: Returns the current option in the drop-down box.
  • select_by_value( value)Method: Select the option according to the value attribute of the option.
  • select_by_index( index)Method: Select the option according to the index of the option.
  • select_by_visible_text( text)Method: Select the option according to the text of the option.
  • deselect_all()Method: Cancel all the selected options, and only take effect for the drop-down box that supports multiple selection .
  • deselect_by_value(value)Method: Cancel the selected option according to the value attribute of the option, and it will only take effect for the drop-down box that supports multiple selection .
  • deselect_by_index(self, index)Method: Cancel the selected option according to the index of the option, and it will only take effect for the drop-down box that supports multiple selection .
  • deselect_by_visible_text(text)Method: Cancel the selected option according to the text of the option, and it will only take effect for the drop-down box that supports multiple selection .

Case study

import selenium.webdriver as webdriver
from selenium.webdriver.support.select import Select

driver = webdriver.Firefox()
driver.get("https://kns.cnki.net/kns/brief/result.aspx?dbprefix=scdb")
# 定位下拉框元素
select_element = driver.find_element_by_id("txt_1_sel")
# 实例化Select对象
select_object = Select(select_element)
# 检测是否支持多选
print(select_object.is_multiple)
# 输出所有选项文本
print([i.text for i in select_object.options])
# 输出所有选项value属性
print([i.get_attribute("value") for i in select_object.options])
# 输出所有已选选项value属性
print([i.get_attribute("value") for i in select_object.all_selected_options])
# 输出当前已选选项value属性
print(select_object.first_selected_option.get_attribute("value"))
# 选择索引为2的选项
select_object.select_by_index(2)
# 输出当前已选选项value属性
print(select_object.first_selected_option.get_attribute("value"))
# 选择value属性为TI的选项
select_object.select_by_value("TI")
# 输出当前已选选项value属性
print(select_object.first_selected_option.get_attribute("value"))
# 选择文本为全文的选项
select_object.select_by_visible_text("全文")
# 输出当前已选选项value属性
print(select_object.first_selected_option.get_attribute("value"))
driver.quit()

The result is:

None
['主题', '篇关摘', '关键词', '篇名', '摘要', '全文', '被引文献', '中图分类号', 'DOI']
['SU$%=|', 'TKA$%=|', 'KY', 'TI', 'AB', 'FT', 'RF', 'CLC$=|??', 'ZCDOI$=|?']
['SU$%=|']
SU$%=|
KY
TI
FT

Guess you like

Origin blog.csdn.net/mighty13/article/details/115109797