示例:对 form.html 中的复选框和单选按钮进行操作
注:form.html 和 demo03.py 在同一目录下
form.html 代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试 checkbox 和 radio button</title>
</head>
<body>
<form action="javascript:alert('test')">
apple: <input type="checkbox" name="apple" value="apple"><br>
banana: <input type="checkbox" name="banana" value="banana"><br>
mango: <input type="checkbox" name="mango" value="mango"><br>
<hr>
gender: <input type="radio" name="gender" value="male">male
<input type="radio" name="gender" value="male">female<br>
<input type="submit" value="submit">
</form>
</body>
</html>
demo03.py 代码如下
from selenium import webdriver
from time import sleep
from os import path
from os.path import join
class TestCase(object):
def __init__(self):
demo03_path = path.dirname(path.abspath(__file__))
form_path = join(demo03_path, 'form.html')
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.get(form_path)
def test_checkbox(self):
"""
测试 checkbox 的方法
:return:
"""
apple = self.driver.find_element_by_name('apple')
if not apple.is_selected():
apple.click()
banana = self.driver.find_element_by_name('banana')
if not banana.is_selected():
banana.click()
sleep(2)
def test_radio_button(self):
"""
测试 radio 的方法
:return:
"""
self.driver.find_elements_by_name('gender')[1].click()
if __name__ == '__main__':
case = TestCase()
case.test_checkbox()
case.test_radio_button()