网络编程
位置:首页>> 网络编程>> Python编程>> python自动化测试selenium操作checkbox和radiobox技术

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

作者:aovenus  发布时间:2021-11-01 16:12:54 

标签:自动化测试,selenium,checkbox,radiobox

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

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

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

 示例页面:

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

页面代码:


<!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()

运行结果:

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

来源:https://blog.csdn.net/aovenus/article/details/121198280

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com