Selenium2+python Automation 19-Radio box and check box

Source: Internet
Author: User

This article mainly introduces the operation of Radio box and check box

I. Recognize the radio box and check box

1. Recognize what the Radio box and check box look like first

2. The small partners to see clearly, the above box is round, the check box is square, this is the industry standard, if the development of the small partner to the wrong icon, you can first smoked him.

Second, radio and CheckBox source code

1. The HTML source as follows, the following paragraph complex down, write to the text, the suffix changed to. html.

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title> Radio and check </title>
<body>

</form>
<form>
<label value= "Radio" > Men </label>
<input name= "Sex" value= "male" id= "Boy" type= "Radio" ><br>
<label value= "Radio1" > Women </label>
<input name= "Sex" value= "female" id= "Girl" type= "Radio" >
</form>

<form>
<!--<labelfor= "C1" >checkbox1</label>-
<input id= "C1" type= "checkbox" >selenium<br>
<!--<labelfor= "C2" >checkbox2</label>-
<input id= "C2" type= "checkbox" >python<br>
<!--<labelfor= "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>

Third, radio: Radio

1. First, Position the selection box

2. Locate the ID, click on the icon, the code is as follows (get URL address method: Paste the above source into the text saved as. html suffix after the browser opens, in the browser URL address bar to copy the address on it)

3. First click on Boy, wait 10 seconds and then click Girl, observe the page changes

Four, check box: checkbox

1. Tick a single box, such as tick selenium this, can be based on its ID=C1 direct positioning to click on it

2. So the question is: If you want to check all of them?

Five, all check:

1. All tick, you can use to locate a set of elements, from the above source can be seen, check box Type=checkbox, here can be used XPath syntax:.//*[@type = ' checkbox ']

2. Notice here, knocking on the blackboard to take notes:find_elements is not directly click, it is plural, so can only get all the CheckBox object, and then through the for loop to one click operation

Six, judge whether to select: Is_selected ()

1. Sometimes this option box, itself is selected, if I click again, it is reversed, this is not the result I expected, then can I click when it is not selected, when it is already selected, I do not click it? So here's the question: How do I tell if the option box is selected?

2. Determine whether the element is selected this step is the core of this article, click the Options box for everyone is not difficult. Gets whether the element is selected and prints the result as.

3. Return the result to type bool, return False when not clicked, return True after click, then it is easy to judge, can be used as the judgment before the operation, but also as the test result judgment

Seven, reference code:
# Coding:utf-8
From selenium import Webdriver
Driver = Webdriver. Firefox ()
Driver.get ("file:///C:/Users/Gloria/Desktop/checkbox.html")
# to determine the status of the option box before clicking the action
s = driver.find_element_by_id ("Boy"). is_selected ()
Print S
DRIVER.FIND_ELEMENT_BY_ID ("Boy"). Click ()
# Click to determine if the element is selected
r = driver.find_element_by_id ("Boy"). is_selected ()
Print R

# check box single 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 ()

Selenium2+python Automation 19-Radio box and check box

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.