-----The core of automated testing
Object positioning should be the core of automated testing, in order to manipulate an object, you should first identify the object. An object is a person, he will have a variety of characteristics (attributes), such as we can through a person's ID number, name, or he lives in which street, floor, number of people find this person.
Then an object has a similar attribute, and we can find this object through this property.
There are several common purposes for locating objects
· Manipulating objects
· Gets the properties of the object, such as getting the class property of the test object, the Name property, and so on
· Get the object's text
· Get the number of objects
Webdriver provides a series of object positioning methods, commonly used in the following several
· Id
· Name
· Class name
· Link text
· Partial link text
· Tag name
· Xpath
· CSS Selector
We can see that a Baidu input box can be used in such a way to locate.
#coding =utf-8 from
Selenium import webdriver
import time
browser = Webdriver. Firefox ()
browser.get ("http://www.baidu.com")
Time.sleep (2)
######## #百度输入框的定位方式 ##########
# Locate
browser.find_element_by_id ("kw") by ID. Send_keys ("Selenium")
#通过name方式定位
Browser.find_element_ By_name ("WD"). Send_keys ("Selenium")
locates
browser.find_element_by_tag_name ("input") #通过tag name way. Send_ The keys ("Selenium")
locate
browser.find_element_by_class_name ("S_ipt") #通过class the name method. Send_keys ("Selenium")
#通过CSS方式定位
browser.find_element_by_css_selector ("#kw"). Send_keys ("Selenium")
#通过xphan方式定位
Browser.find_element_by_xpath ("//input[@id = ' kw ']"). Send_keys ("Selenium")
########################## ##################
browser.find_element_by_id ("su"). Click ()
time.sleep (3)
browser.quit ()
ok~! By using one of the examples above, we have shown several ways of positioning, and here's how to do each:
ID and name
ID and name are the most commonly used positioning methods, because most controls have these two properties and are generally meaningful and have different names when naming the control's ID and name. These two properties make it fairly easy to find a property on a page
We used the front-end tool to find the Baidu input Box property information, as follows:
<input id= "kw" class= "S_ipt" type= "text" maxlength= "" name= "WD" autocomplete= "Off" >
id= "KW"
Through the find_element_by_id ("kw") function is to capture the Baidu input box
Name= "WD"
The Find_element_by_name ("WD") function can also capture the Baidu input box
Tag name and class name
From the above Baidu input box attribute information, we see, not only ID and name two attributes, such as class and tag name (sign)
<input>
Input is the name of a label that can be positioned through the find_element_by_tag_name ("input") function.
Class= "S_ipt"
Capture the Baidu input box through the Find_element_by_class_name ("S_ipt") function.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/