1 HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h3 style="color: brown">radio框</h3>
<div id="s_radio">
<input type="radio" name="radios" value="radio1">radio1<br>
<input type="radio" name="radios" value="radio2">radio2<br>
<input type="radio" name="radios" value="radio3" checked="checked">radio3
</div>
<hr>
<h3 style="color: brown">checkbox框</h3>
<div id="s_checkbox">
<input type="checkbox" name="checkboxs" value="checkbox1">checkbox1<br>
<input type="checkbox" name="checkboxs" value="checkbox2">checkbox2<br>
<input type="checkbox" name="checkboxs" value="checkbox3" checked="checked">checkbox3
</div>
<hr>
<h3 style="color: brown">select框</h3>
<h4 style="color: rgb(22, 118, 173)">单选</h4>
<select id="ss_single">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3" selected="selected">option3</option>
</select>
<hr>
<h4 style="color: rgb(22, 118, 173)">多选</h4>
<select id="ss_multi" multiple>
<option value="options1">options1</option>
<option value="options2">options2</option>
<option value="options3" selected="selected">options3</option>
</select>
</body>
</html>
2 radio框
定位元素后点击即可实现radio框选择
Radio = broswer.find_elements(By.ID,"s_radio")
Radio.find_element(By.XPATH,'./input[@value="radio2"]').click()
3 checkbox框
定位元素,取消checkbox框所有选择的选项,并选择指定选项
Checkboxes=broswer.find_element(By.XPATH,'//*[@id="s_checkbox"]/input[@checked="checked"]')
for Checkbox in Checkboxes:
Checkbox.click()
browser.find_element(By.XPATH,'//*[@id="s_checkbox"]/input[@value="checkbox2"]').click()
4 select框
1 单选框
select_by_index:通过索引选择选项 select_by_visible_text:通过可视文本选择选项 select_by_value:通过属性value选择选项
from selenium.webdriver.support.select import Select
select = Select(browser.find_element(By.ID,'ss_single'))
select.select_by_index(0)
select.select_by_visible_text("option2")
select.select_by_value("option3")
2 多选框
deselect_by_index:通过索引取消选择选项 deselect_by_visible_text:通过可视文本取消选择选项 deselect_by_value:通过属性value取消选择选项 deselect_all:取消所有选项
select = Select(browser.find_element(By.ID,'ss_multi'))
select.select_by_index(0)
select.select_by_visible_text("options2")
select.select_by_value("options3")
select.deselect_by_index(0)
select.deselect_by_visible_text("options2")
select.deselect_by_value("options3")
select.deselect_all()