Selenium2+python automation 19-radiobox and checkbox (radiobox, checkbox)

This article mainly introduces the operation of radio buttons and checkboxes

1. Recognize radio buttons and checkboxes

    1. First recognize what radio buttons and checkboxes look like

 

 

    2. Dear friends, see clearly, the radio button above is round; the checkbox in the picture below is square, which is the industry standard. If the developer makes a mistake in the icon, you can choose him first.

Second, radio and checkbox source code

    1. The html source code of the above picture is as follows, complicate the following paragraph, write it in the text, and change the suffix to .html.

 <html>  
    <head>  
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />  
    <title>单选和复选</title>  
    </head>  
    <body>  
    
    </form>  
    <h4>单选:性别</h4>   
    <form>  
    <label value="radio">男</label>   
    <input name="sex" value="male" id="boy" type="radio"><br>  
    <label value="radio1">女</label>  
    <input name="sex" value="female" id="girl" type="radio">  
    </form>  
    
    <h4>微信公众号:从零开始学自动化测试</h4>  
    <form>  
    <!-- <label for="c1">checkbox1</label> -->  
    <input id="c1" type="checkbox">selenium<br>  
    <!-- <label for="c2">checkbox2</label> -->  
    <input id="c2" type="checkbox">python<br>  
    <!-- <label for="c3">checkbox3</label> -->  
    <input id="c3" type="checkbox">appium<br>  
    
    <!-- <form>  
    <input type="radio" name="sex" value="male" /> Male  
    <br />  
    <input type="radio" name="sex" value="female" /> Female  
    </form> -->  
      
    </body>  
    </html> 

Three, radio: radio

  1. The first is to locate the position of the selection box

 

 

  2. Locate the id, click the icon, and the code is as follows (the method of obtaining the url address: paste the above source code into the text and save it as a .html suffix, open it with a browser, and copy the address in the url address bar of the browser)

  3. After clicking boy first, wait ten seconds and then click girl to observe the page changes

 

Fourth, the checkbox: checkbox

  1. Check a single box, such as checking selenium, you can directly locate and click according to its id=c1

 

   2. So here comes the question: what if you want to check all of them?

5. Check all:

    1. Check all, you can use to locate a group of elements, as can be seen from the above source code, the type=checkbox of the checkbox, here you can use the xpath syntax: .//*[@type='checkbox']

 

     2. Note here, tap the blackboard to take notes: find_elements cannot be clicked directly, it is plural, so you can only get all the checkbox objects first, and then go through the for loop to click one by one

6. Determine whether it is selected: is_selected()

    1. Sometimes the option box itself is selected. If I click it again, it will be deselected. This is not the result I expected, so can I click it when it is not selected? It is already selected, so I won't click it? So the question is: how to judge whether the option box is selected?

    2. Judging whether the element is selected This step is the core content of this article. Clicking the option box is not difficult for everyone. Gets whether the element is in the selected state, and the print result is as shown below.

    3. The returned result is bool type. It returns False when not clicked, and returns to True after clicking. Then it is easy to judge, which can be used as a judgment before the operation or as a judgment of the test result.

 

7. Reference code:
# coding:utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("file:///C:/Users/Gloria/Desktop/checkbox.html")
# No click operation Before, judge the state of the option box
s = driver.find_element_by_id("boy").is_selected()
print s
driver.find_element_by_id("boy").click()
# After clicking, judge whether the element is in the selected state
r = driver.find_element_by_id( "boy").is_selected()
print r

# Check box radio selection
driver.find_element_by_id("c1").click()
# Check box select all
checkboxs = driver.find_elements_by_xpath(".//*[@type=' checkbox']")
for i in checkboxs:
    i.click()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325343221&siteId=291194637