selenium操作下拉列表

在这里插入图片描述
代码如下:
前端代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="javascript:alert('test')">
    provide:
    <select name="provise" id="provise">
        <option value="bj">BeiJing</option>
        <option value="sh">ShangHai</option>
        <option value="zz">ZhengZhou</option>

    </select>
</form>

</body>
</html>

操作下拉框代码:

from selenium import webdriver
from time import sleep
from selenium.webdriver.support.select import Select
import os
class testcase(object):
    def __init__(self):
        self.driver=webdriver.Chrome()
        # 获取绝对路径地址
        patch = os.path.dirname(os.path.abspath(__file__))
        print(patch)
        # 拼接地址
        file_patch = 'file:///' + patch + '/forms3.html'
        print(file_patch)
        self.driver.get(file_patch)

    def test_select(self):
        """下拉框选择想要的数据"""
        se= self.driver.find_element_by_id('provise')
        select=Select(se)
        select.select_by_index(2)
        sleep(2)
        select.select_by_value('bj')
        sleep(2)
        select.select_by_visible_text('ShangHai')
        sleep(2)
        self.driver.quit()

    def test_select_all(self):
        """下拉框全选后再反选"""
        se = self.driver.find_element_by_id('provise')
        select = Select(se)
        for i in range(3): #全选
            select.select_by_index(i)
            sleep(1)
        sleep(3)
        select.deselect_all() #反选
        sleep(3)

    def test_select_option(self):
        """打印出来下拉框数据"""
        se = self.driver.find_element_by_id('provise')
        select = Select(se)
        for option in select.options:
            print(option.text)


if __name__ == '__main__':
    case=testcase()
    #case.test_select()
    #case.test_select_all()
    case.test_select_option()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值