First, write yourself a html, contains three check box, as shown below:
HTML code is as follows:
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>checkbox</title> </head> <body> <h3>checkbox</h3> <div class="well"> <form class="form-horizontal"> <div class="control-group"> <label class="control-label" for="c1">checkbox1</label> <div class="controls"> <input type="checkbox" id="c1" /> </div> </div> <div class="control-group"> <label class="control-label" for="c2">checkbox2</label> <div class="controls"> <input type="checkbox" id="c2" /> </div> </div> <div class="control-group"> <label class="control-label" for="c3">checkbox3</label> <div class="controls"> <input type="checkbox" id="c3" /> </div> </div> </form> </div> </body> </html>
Now, come check this box 3
Positioning a set of elements with the foregoing method analogous added only after the element s, to position a set of elements, for example: find_element S _by_id ()
. 1 from Selenium Import the webdriver 2 Import Time, OS . 3 . 4 Driver = webdriver.Chrome () . 5 # path.abspath () method for obtaining the file in the current directory . 6 file_path = ' File: /// ' + the os.path. ABSPATH ( ' checkbox.html ' ) . 7 driver.get (file_path) . 8 . 9 checkboxes = driver.find_elements_by_xpath ( " // INPUT [@ type = 'checkBox'] " ) # find box 10 . 11 for chekbox incheckboxes: # traversing each of a box, and click the check 12 is chekbox.click () 13 is the time.sleep (. 1 ) 14 15 driver.quit ()
You can also find and check the elements as follows:
inputs = driver.find_elements_by_tag_name('input') for i in inputs: if i.get_attribute('type') == 'checkbox': i.click() time.sleep(1)
In addition, other operations may also be:
Print (len (checkboxes)) # Print box number driver.find_elements_by_xpath ( " // INPUT [@ type = 'CheckBox'] " ) .pop (). the Click () # hook the last check box remove selected
pop () method for obtaining a certain element in the list, the default is the last one, and returns the value of the element. For a set of elements of one element may be checked as follows:
- pop (0) first
- pop (1) a second
- ....
- pop () or pop (-1) last