http://www.testclass.net/ Test Tutorial Network, professional Selenium learning website.
This sectionto solves the problem:
How do I locate a set of elements?
Scene
As you can see from the example in the previous section, Webdriver can easily use the Findelement method to locate a particular object, but sometimes we need to locate a group of objects,
This is the time to use the Findelements method.
Locating a group of objects is typically used in the following scenarios:
· Bulk manipulate objects, such as checking all checkboxes on a page
· Gets a set of objects, and then filters out objects in this set of objects that need to be specifically targeted. For example, locate all the checkboxes on the page and select the last
<Html><Head><MetaHttp-equiv= "Content-type"Content= "Text/html;charset=utf-8"/><Title>checkbox</Title><ScriptType= "Text/javascript"Async=""Src= "Https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></Script><LinkHref= "Http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"Rel= "stylesheet"/><ScriptSrc= "Http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></Script></Head><Body><H3>checkbox</H3><DivClass= "Well"><FormClass= "Form-horizontal"><DivClass= "Control-group"><LabelClass= "Control-label"For= "C1">checkbox1</Label><DivClass= "Controls"><InputType= "checkbox"Id= "C1"/></Div></Div><DivClass= "Control-group"><LabelClass= "Control-label"For= "C2">checkbox2</Label><DivClass= "Controls"><InputType= "checkbox"Id= "C2"/></Div></Div><DivClass= "Control-group"><LabelClass= "Control-label"For= "C3">checkbox3</Label><DivClass= "Controls"><InputType= "checkbox"Id= "C3"/></Div></Div><DivClass= "Control-group"><LabelClass= "Control-label"For= "R">radio</label> <div class= "Controls" > <input type= "Radio" Id= "R1" /> </div> </div> <div class=" Control-group "> <label class=" Control-label "for=" R ">radio</label> <div class=" Controls "> < Input type= "Radio" id= "R2"/> </div> </div> </form> </div> </body>
Copy this code to Notepad and save it as a checkbox.html file. (Note that this page needs to be placed in the same directory as our automation script)
The first method:
We can see three check boxes and two radio boxes on this page through our browser. Let's locate the three check boxes below.
#-*-Coding:utf-8-*-From seleniumImportWebdriverImportTimeImportOSDR =Webdriver. Firefox () File_path =‘file:///' + Os.path.abspath (‘checkbox.html ") Dr.get ( File_path) # Select all the input on the page, Then filter out all the checkboxes and tick the inputs = Dr.find_elements_by_tag_name (input ") for input in inputs: if input.get_attribute (" type") = = "checkbox ' : Input.click () time.sleep (2
You can try to change the checkbox in Input.get_attribute (' type ') = = ' checkbox ' into Radio , which will be two radio boxes.
The second method of positioning:
#-*-Coding:utf-8-*-From seleniumImportWebdriverImportTimeImportOSDR =Webdriver. Firefox () File_path =‘file:///' + Os.path.abspath (‘Checkbox.html ") dr.get (file_path) #< Span style= "COLOR: #008000" > Select all the checkboxes and tick them all checkboxes = Dr.find_elements_by_css_selector ( input[type=checkbox]" ) for checkbox in Checkboxes:checkbox.click () time.sleep (2) # print how many Checkboxprint Len (dr.find_) on the current page Elements_by_css_selector ( ' input[type=checkbox] '
The second way of writing is not the same as the first one, it is a loop to check the control; If you learn the last chapter, careful you must find that the positioning function is different,
The first uses the name , the second type of CSS.
How to remove the tick:
There is also a problem, sometimes we do not want to tick all the page check box (checkbox), you can use the following method to remove the last tick box. As follows:
#-*-Coding:utf-8-*-From seleniumImportWebdriverImportTimeImportOSDR =Webdriver. Firefox () File_path =‘file:///' + Os.path.abspath (‘Checkbox.html ") dr.get (file_path) #< Span style= "COLOR: #008000" > Select all the checkboxes and tick them all checkboxes = Dr.find_elements_by_css_selector ( input[type=checkbox]" ) for checkbox in Checkboxes:checkbox.click () time.sleep (2) # remove the Dr.find_elements_by_css_selector ( ' input[type=checkbox]"
In fact, the removal of the tick list is also very simple logic, that is, click the Check button again. Perhaps we are confused about how to find the "last" button. pop () can implement this function.
All right! In the learning process of web automation, we have to know something about the front-end, here to expand:
Http://www.w3school.com.cn/js/jsref_pop.asp
Try :
Put Find_elements_by_css_selector (' input[type=checkbox] '). The checkbox in the Pop (). Click () becomes the radio effect, Try it yourself!
--------------------------
Learn more about selenium content:
Functional Test Automation Rollup
Easy Automation---selenium-webdriver (python) (iv)