1. Select the box
Html codes used in this chapter:
<!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>
1.1 radio box
radio box to select options directly click method WebElement to simulate the user clicks on it.
For example, we want to in the following html:
First print the currently selected value
Then select radio2
<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>
Corresponding code is as follows:
from Selenium Import the webdriver WD = webdriver.Chrome (R & lt ' E: \ webdrivers \ chromedriver.exe ' ) wd.implicitly_wait ( 10 ) wd.get ( ' http://127.0.0.1:8020/day01/index.html ' ) # Get the currently selected element element = wd.find_element_by_css_selector ( ' #s_radio INPUT [= the checked the checked] ' ) Print ( ' is currently selected: ' + element.get_attribute ( ' value ' )) # tap Radio2 wd.find_element_by_css_selector ('#s_radio input[value="radio2"]').click()
1.2 checkbox box
Of the checkbox is selected, but also directly click method WebElement to simulate the user clicks to select.
Note that, an option to select the checkbox, you must first obtain the current status of the check box if the option is checked, you can not click. Otherwise, it will cancel the selection.
For example, we want the following html: Check checkbox2
<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>
Our idea can be like this:
Put all the options you have selected click, to ensure that all non-selected state
Then click checkbox2
Sample code:
from Selenium Import the webdriver WD = webdriver.Chrome (R & lt ' E: \ webdrivers \ chromedriver.exe ' ) wd.implicitly_wait ( 10 ) wd.get ( ' http://127.0.0.1:8020/day01/index.html ' ) # put all the options you have selected click Elements = wd.find_elements_by_css_selector ( ' #s_checkbox the INPUT [= the checked "the checked"] ' ) for Element in Elements: element.click () # click CheckBox2 wd.find_element_by_css_selector ( " #s_checkbox input [value = 'checkbox2']").click()
1.3 select box
radio box and checkbox input elements are box, just inside the type is different.
select box is select a new label, we can control the browser page content check.
For Select selection box, Selenium specifically provides a select class operation.
1.3.1 common methods
Select class provides the following method:
(1) select_by_value
According to value the property value of the options, select elements.
For example, the following HTML:
<option value="foo">Bar</option>
You can select the option based on the value foo:
s.select_by_value('foo')
(2) select_by_index
According to the order of the options (beginning from 1), select elements.
(3) select_by_visible_text
According visible text options, select elements.
For example, the following HTML:
<option value="foo">Bar</option>
Bar will be based on the content, select this option:
s.select_by_visible_text('Bar')
(4) deselect_by_value
According to value property value of the option to remove the selected element.
(5) deselect_by_index
According to the order of the options to remove the selected element.
(6) deselect_by_visible_text
According visible text option to remove the selected element.
(7) deselect_all
Remove all selected elements.
1.3.2 Select checkbox
For select radio button, operation is relatively simple:
Whatever the original election is that you can use to directly select the Select method.
For example, select the sample inside option2, the following sample code:
from selenium import webdriver wd = webdriver.Chrome(r'E:\webdrivers\chromedriver.exe') wd.implicitly_wait(10) wd.get('http://127.0.0.1:8020/day01/index.html') # 导入Select类 from selenium.webdriver.support.ui import Select # 创建Select对象 select = Select(wd.find_element_by_id("ss_single")) # 通过 Select 对象选中option2 select.select_by_visible_text("option2")
1.3.3 Select checkbox
For select the checkbox to select a few options to pay attention to remove the already selected option.
For example, we have chosen the example of options2 checkbox and options3.
Deselect_all method can select the class, has been selected to clear all of the options.
And then select options2 options3 by select_by_visible_text method.
Sample code is as follows:
from Selenium Import the webdriver WD = webdriver.Chrome (R & lt ' E: \ webdrivers \ chromedriver.exe ' ) wd.implicitly_wait ( 10 ) wd.get ( ' http://127.0.0.1:8020/day01/index.html ' ) # import select class from selenium.webdriver.support.ui import select # create objects select the sELECT = select (wd.find_element_by_id ( " ss_multi " )) # Clear all been selected option select.deselect_all () # select options2 and options3 the sELECT. select_by_visible_text ("options2") select.select_by_visible_text("options3")