1 HTML代码


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <h3 style="color: brown">radio框</h3>
  9. <div id="s_radio">
  10. <input type="radio" name="radios" value="radio1">radio1<br>
  11. <input type="radio" name="radios" value="radio2">radio2<br>
  12. <input type="radio" name="radios" value="radio3" checked="checked">radio3
  13. </div>
  14. <hr>
  15. <h3 style="color: brown">checkbox框</h3>
  16. <div id="s_checkbox">
  17. <input type="checkbox" name="checkboxs" value="checkbox1">checkbox1<br>
  18. <input type="checkbox" name="checkboxs" value="checkbox2">checkbox2<br>
  19. <input type="checkbox" name="checkboxs" value="checkbox3" checked="checked">checkbox3
  20. </div>
  21. <hr>
  22. <h3 style="color: brown">select框</h3>
  23. <h4 style="color: rgb(22, 118, 173)">单选</h4>
  24. <select id="ss_single">
  25. <option value="option1">option1</option>
  26. <option value="option2">option2</option>
  27. <option value="option3" selected="selected">option3</option>
  28. </select>
  29. <hr>
  30. <h4 style="color: rgb(22, 118, 173)">多选</h4>
  31. <select id="ss_multi" multiple>
  32. <option value="options1">options1</option>
  33. <option value="options2">options2</option>
  34. <option value="options3" selected="selected">options3</option>
  35. </select>
  36. </body>
  37. </html>

2 radio框


定位元素后点击即可实现radio框选择

  1. Radio = broswer.find_elements(By.ID,"s_radio")
  2. Radio.find_element(By.XPATH,'./input[@value="radio2"]').click()

3 checkbox框


定位元素,取消checkbox框所有选择的选项,并选择指定选项

  1. Checkboxes=broswer.find_element(By.XPATH,'//*[@id="s_checkbox"]/input[@checked="checked"]')
  2. for Checkbox in Checkboxes:
  3. Checkbox.click()
  4. 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选择选项

  1. from selenium.webdriver.support.select import Select
  2. select = Select(browser.find_element(By.ID,'ss_single'))
  3. select.select_by_index(0)
  4. select.select_by_visible_text("option2")
  5. select.select_by_value("option3")

2 多选框

deselect_by_index:通过索引取消选择选项 deselect_by_visible_text:通过可视文本取消选择选项 deselect_by_value:通过属性value取消选择选项 deselect_all:取消所有选项

  1. select = Select(browser.find_element(By.ID,'ss_multi'))
  2. select.select_by_index(0)
  3. select.select_by_visible_text("options2")
  4. select.select_by_value("options3")
  5. select.deselect_by_index(0)
  6. select.deselect_by_visible_text("options2")
  7. select.deselect_by_value("options3")
  8. select.deselect_all()