以下两种导的包没有任何本质区别。ui包就是调用Select包中的select方法。
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import Select
**选择下拉选项:**select类提供了3种方法某一选项的方法,分别是:
- 通过index选择下拉选项——select_by_index(index);
- 通过value值选择下拉选项——select_by_value(value);
- 通过可见的text选择下拉选项——select_by_visible_text(text)。
**取消下拉选项:**select类提供了4种方法某一选项的方法,分别是:
- 通过index选择下拉选项——deselect_by_index(index);
- 通过value值选择下拉选项——deselect_by_value(value);
- 通过可见的text选择下拉选项——deselect_by_visible_text(text);
- 全部取消——deselect_all()。
返回选项:
- 返回所有选项——options;
- 返回第一个选项——first_selected_option;
- 返回所有选中项——all_selected_options。
判断select选项是否可多选:
- is_multiple,返回一个布尔值
选择下拉框的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>select 下拉列表</title>
</head>
<body>
<select id = "S1ID">
<option></option>
<option value ="o1" id = "id1">o1</option>
<option value ="o2" id = "id2">o2</option>
<option value ="o3" id = "id3">o3</option>
</select>
</body>
</html>
from selenium import webdriver
from selenium.webdriver.common.by import By
import os
from time import sleep
file_path = os.getcwd()+os.sep+"myhtml6_2.html"
driver = webdriver.Chrome()
driver.get(file_path)
s1 = Select(driver.find_element(By.ID,"S1ID"))
s1.select_by_index(1)
sleep(2)
s1.select_by_value("o3")
sleep(2)
s1.select_by_visible_text("o2")
sleep(2)
driver.quit()
Note:
- index从0开始
- value是<option>标签的一个属性值,并不是显示在下拉列表中的值
- visible_text是在<option>和<\option>标签对中间的值,是显示在下拉列表中的值
取消下拉框的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>可以多选select 下拉列表</title>
</head>
<body>
<select id = "S1ID" multiple = "multiple">
<option>请选择:</option>
<option value ="o1" id = "id1">o1</option>
<option value ="o2" id = "id2">o2</option>
<option value ="o3" id = "id3">o3</option>
</select>
</body>
</html>
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep
import os
file_path = os.getcwd()+os.sep+"myhtml6_3.html"
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(file_path)
ele = Select(driver.find_element(By.ID,"S1ID"))
ele.select_by_index(1)
sleep(2)
ele.deselect_by_index(1)
sleep(2)
ele.select_by_value("o2")
sleep(2)
ele.deselect_by_value("o2")
sleep(2)
ele.select_by_visible_text("o3")
sleep(2)
ele.deselect_by_visible_text("o3")
sleep(2)
ele.select_by_value("o1")
ele.select_by_value("o2")
sleep(2)
ele.deselect_all()
sleep(2)
driver.quit()
返回选项
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium import webdriver
from time import sleep
import os
file_path = os.getcwd()+os.sep+"myhtml6_3.html"
drvier = webdriver.Chrome()
drvier.maximize_window()
drvier.get(file_path)
select = Select(drvier.find_element(By.ID,"S1ID"))
sleep(2)
all_options = select.options
print("返回的是:",all_options)
print("")
print("返回数据的数据类型是:{}".format(type(all_options)))
mylist = []
for i in all_options:
mylist.append(i.text)
print(mylist)
drvier.quit()
输出结果
返回的是: [<selenium.webdriver.remote.webelement.WebElement (session="cace0fdb5748863a770aba459cbbaf42", element="9f7c4c28-e152-4575-be75-1a95538a5f18")>, <selenium.webdriver.remote.webelement.WebElement (session="cace0fdb5748863a770aba459cbbaf42", element="e3b4ae56-6384-4a61-88e2-b37aa233f39b")>, <selenium.webdriver.remote.webelement.WebElement (session="cace0fdb5748863a770aba459cbbaf42", element="64250962-25af-43f4-958a-92db65eaeaaf")>, <selenium.webdriver.remote.webelement.WebElement (session="cace0fdb5748863a770aba459cbbaf42", element="aa86e27b-3b98-453b-b74d-1282069f0591")>]
返回数据的数据类型是:<class 'list'>
['请选择:', 'o1', 'o2', 'o3']
Note:
opions返回的是选项元素,想要获取文字,需要时”.text”
练习需求:循环选择o1,o2,o3
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep
import os
file_path = os.getcwd()+os.sep+"myhtml6_3.html"
driver = webdriver.Chrome()
driver.get(file_path)
select = Select(driver.find_element(By.ID,"S1ID"))
all_options = select.options
for i in range(0,len(all_options)):
if i == 0:
continue
select.select_by_index(i)
sleep(2)
driver.quit()
获取第一个选项的文字:.first_selected_option
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep
import os
file_path = os.getcwd()+os.sep+"myhtml6_3.html"
driver = webdriver.Chrome()
driver.get(file_path)
select = Select(driver.find_element(By.ID,"S1ID"))
all_options = select.options
select.select_by_index(2)
select.select_by_index(1)
select.select_by_index(3)
print(select.first_selected_option.text)
driver.quit()
输出结果:o1
Note:我们会发现不管先选中的时哪一个选项,使用first_selected_option方法时返回的都是最上面那个选项。文章来源:https://www.toymoban.com/news/detail-754289.html
from selenium.webdriver.support.select import Select
from selenium import webdriver
import os
from time import sleep
file_path = os.getcwd()+os.sep+"myhtml6_3.html"
drvier = webdriver.Chrome()
drvier.get(file_path)
ele = Select(drvier.find_element_by_id("S1ID"))
res = ele.is_multiple
print(res)
drvier.quit()
下面代码中第一个option的text开头有三个空格,第二个option的text中间包含三个空格,第三个option的text中包含了3个’ ’。在HTML中“ ”和空格的区别是,在HTML中没输入一个转义字符’ ’就表示一个空格,输入10个页面就显示10个空格位置。而在HTML代码中不管输入多少个空格,最终页面中显示的空格就只有一个文章来源地址https://www.toymoban.com/news/detail-754289.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>空格示例</title>
</head>
<body>
<select id = "S1ID" multiple ="multiple">
<option>请选择:</option>
<option value ="o1" id = "id1"> With spaces</option>
<option value ="o2" id = "id2">abc With spaces</option>
<option value ="o3" id = "id3">abc With nbsp</option>
</select>
</body>
</html>
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
import os
from time import sleep
file_path = os.getcwd() + os.sep + "myhtml6_4.html"
driver = webdriver.Chrome()
driver.get(file_path)
sele1 = Select(driver.find_element_by_id("S1ID"))
sele1.select_by_visible_text('With spaces')
sleep(2)
sele1.select_by_visible_text('abc With spaces')
sleep(2)
sele1.select_by_visible_text("abc With nbsp")
sleep(2)
driver.quit()
到了这里,关于selenium:select下拉框的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!