如何在静态下拉列表中获取所有选项?
我们可以在 Selenium 中获得下拉列表中的所有选项。下拉列表中的所有选项都存储在一个列表数据结构中。这是借助于 Select 类中的一个方法 options() 实现的。
options() 返回 select 标记下所有选项的列表。如果未在页面上使用任何定位器标识下拉列表,则返回一个空列表。
语法
d = Select(driver.find_element_by_id("selection"))
o = d.options()例子
获取下拉列表中所有选项的代码实现。
from selenium import webdriver
from selenium.webdriver.support.select import Select
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://tutorialspoint.com/tutor_connect/index.php")
#to refresh the browser
driver.refresh()
#select class provide the methods to handle the options in dropdown
d = Select(driver.find_element_by_xpath("//select[@name='seltype']"))
#store the options in a list with options method
l = d.options
#to count the number of options and print in console
print(len(l))
#to get all the number of options and print in console
for i in l:
print(i.text)
#to close the browser
driver.close()
广告
数据结构
网络技术
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP