selenium操作checkbox和radiobox python自动化测试selenium操作checkbox和radiobox技术

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

selenium操作checkbox和radiobox python自动化测试selenium操作checkbox和radiobox技术

aovenus   2021-11-13 我要评论
想了解python自动化测试selenium操作checkbox和radiobox技术的相关内容吗,aovenus在本文为您仔细讲解selenium操作checkbox和radiobox的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python,自动化测试,selenium操作checkbox和radiobox,下面大家一起来学习吧。

form表单中经常涉及复选框(checkbox)和单选框(radiobox),如用户的爱好跑步、游泳、跳舞可以使用复选框,性别男、女可以使用单选框。

(1)checkbox选择或反选:使用click()方法

(2)radiobox有相同的名称,多个值,可先通过名称获得,再通过值判断,选择使用click()方法。

 示例页面:

页面代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="javascript:alert('selenium操作表单中checkbox和radiobutton')">
    测试from表单操作checkbox和radiobutton<br>
    跑步:<input type="checkbox" name="running" value="running"><br>
    游泳:<input type="checkbox" name="swimming" value="swimming"><br>
	跳舞:<input type="checkbox" name="dancing" value="dancing"><br>
	
<!--<hr>分界线-->
    <hr>
    性别:<br>
    男:<input type="radio" name="gender" value="male"><br>
    女:<input type="radio" name="gender" value="female"><br> 
    <input type="submit" value="login">
</form>
</body>
</html>

示例脚本:

import os 
from selenium import webdriver
from time import sleep 
class TestCheckBoxOrRadioBtn(object):
    def setup(self):
        self.driver = webdriver.Chrome()
        path = os.path.dirname(os.path.abspath(__file__))
        file_path = 'file:///'+path+'/html/form.html'
        self.driver.get(file_path) 
    def test_checkbox(self):
        #定位跳舞
        dancing=self.driver.find_element_by_name("dancing")
        #如果没有选择,则点击选择
        if not dancing.is_selected():
            dancing.click()
            sleep(2)
        running = self.driver.find_element_by_name("running")
        if not running.is_selected():
            running.click()
            sleep(2)
        swimming = self.driver.find_element_by_name("swimming")
        if not swimming.is_selected():
            swimming.click()
            sleep(2)
        #再次点击取消选择游泳
        swimming.click()
        sleep(2)
        self.driver.quit()
    def test_radio(self):
        #获得元素列表
        gender= self.driver.find_elements_by_name("gender")
        #选中性别男
        gender[0].click()
        sleep(2)
        # 选中性别女
        gender[1].click()
        sleep(2)
        self.driver.quit()
if __name__ == '__main__':
    case = TestCheckBoxOrRadioBtn()
    case.test_checkbox()
    case.test_radio()

运行结果:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们