Selenium web automated testing ---select drop-down box element positioning

Main operations:

1. First locate the select drop-down box element

2. Three ways of option element positioning:

(1) Positioning by attribute value

(2) Positioning through visual text

(3) Positioning by subscript (starting from 0)

3. Need to guide the package: from selenium.webdriver.support.select import Select

# coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.select import Select
import time

driver = webdriver.Chrome(chromedriver.exe路径)
# HTML文件是自己写的,就定义了一个select下拉框,里面有4个option
driver.get("select.html")

time.sleep(3)

# 定位:select下拉框元素
selectEle = driver.find_element_by_id("abc")

# 定位:1-3万
# 通过属性值定位
Select(selectEle).select_by_value("2")
time.sleep(2)

# 定位:3-5万
# 通过可视文本定位
Select(selectEle).select_by_visible_text("3-5万")
time.sleep(2)

# 定位:5-7万
# 通过下标定位,从0开始
Select(selectEle).select_by_index(3)
time.sleep(2)

# 退出浏览器
driver.quit()

Remarks:

select common reference

Guess you like

Origin blog.csdn.net/qq_19982677/article/details/107380141