栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

第7节 Selenium 使用select下拉列表定位

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第7节 Selenium 使用select下拉列表定位

处理下拉列表,需要用到Selenium中的一个工具类Select,以下是常用方法/属性:

方法/属性方法/属性描述
select_by_value()根据值选择
select_by_index()根据索引选择
select_by_visible_text根据文本选择
deselect_by_value根据值反选
deselect_by_index根据索引反选
deselect_by_visible_text根据文本反选
deselect_all反选所有
all_selected_options所有选中选项
first_selected_option第一个选择选项

下面是一个有下拉框(单选)的页面




    
    Title


province:
from selenium import  webdriver
from time import sleep
import os

from selenium.webdriver.support.select import  Select

class TestCase():
    def  __init__(self):
        self.driver = webdriver.Chrome()
        path = os.path.dirname(os.path.abspath(__file__))
        file_path = 'file:///'+path+'/forms4.html'
        self.driver.get(file_path)

#下拉框单选
    def test_select(self):
        se = self.driver.find_element_by_id('province')
        select = Select(se)
        select.select_by_index(1)

        sleep(2)
        select.select_by_value('tj')

        sleep(2)
        select.select_by_visible_text('湖北')

        sleep(2)
        self.driver.quit()


if __name__ == '__main__':
    case = TestCase()
    case.test_select()

下面是一个有下拉框(多选)的页面




    
    Title


province:
from selenium import  webdriver
from time import sleep
import os

from selenium.webdriver.support.select import  Select

class TestCase():
    def  __init__(self):
        self.driver = webdriver.Chrome()
        path = os.path.dirname(os.path.abspath(__file__))
        file_path = 'file:///'+path+'/forms4.html'
        self.driver.get(file_path)

#下拉框多选
    def test_select_all(self):
        se = self.driver.find_element_by_id('province')
        select = Select(se)
        for i in range(4):
            select.select_by_index(i)
            sleep(1)
        sleep(3)
        self.driver.quit()


if __name__ == '__main__':
    case = TestCase()
    case.test_select_all()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/504125.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号