The principle of 1.selenium2
The first one is a brief introduction to the environment configuration of SELENIUM2 based on Python development, which is mainly about basic usage. First, let's talk about the basic principles of Selenium2. Basically knowing how this thing is going, we know how to use it for automated testing.
Unlike selenium and selenium RC, Selenium2 encapsulates the browser's native API into a Webdriver API that can manipulate the elements in the browser page directly, or even manipulate the browser itself (screenshot, window size, start, close, install plugin, Configure a certificate, and so on), so it's like a real user in action. Actually at the beginning we do not need to understand webdriver specific is how the matter, the back will talk about the Webdriver API, and now we just request will use, so know it is enough. The following diagram provides a simple and clear view of the Selenium2 logic.
Webdriver principle
In conclusion, if we want to create our script with Selenium2, we need to do a few things:
Positioning elements
Manipulating elements
Handling Browser events (verification codes, warning boxes, etc.)
Processing system time (uploading files, downloading files, etc.)
This article first tells you how to position elements.
2.Web element Positioning
2.1.Element concept
If you know more about the front end, you can simply interpret the Web interface as a body, and table,element makes up the body. Take Baidu homepage For example, open Baidu homepage, click Input box, right button-"inspect, will see the following front-end code:
Baidu Home Element View
As you can see, the search box has an identity: id= "kw", as for why this, estimated that the development of Baidu is very cool. Then it can be said that the search box is an element.
2.2 Positioning elements
Take the search box as an example, selenium can mimic the user's actions, and can support entering and deleting arbitrary text in the search box. To manipulate an element, locate the element first. As you can see, just the search box element has a number of attributes, such as id,class,name, how do we locate the element? Selenium also supports several methods of positioning? A brief summary is as follows:
by ID
By name
By class name
by tag name
by link
by partial link
by XPath
2.1 Locating by ID
Take Python For example (Python is the default later):
find_element_by_id(‘kw‘)
2.2 Positioning by name
find_element_by_name(‘wd‘)
Like the search button does not use the Name property, then we cannot locate it by using the Name property. If you encounter the element to which the name attribute is anchored, do not forget to change to
find_elements_by_id(‘kname‘)
Otherwise the program will definitely error, the general ID is unique.
2.3 Positioning by class name
Take Baidu search box and search button As an example, the same as:
find_element_by_class_name(‘s_ipt‘)
Know the front-end pro will know, class this element is generally not unique, this time don't forget
lista=find_element_by_class_name(‘classname‘)
All elements of Class name= "ClassName" are captured and stored in the array lista.
2.4 Positioning by Tag name
Tag is a basic attribute of an element, such as the element to be entered as input:
find_elements_by_tag_name(‘input‘)
2.5 Positioning via link,partial link
Baidu Home
Take Baidu News link as an example
find_element_by_link_text(‘新闻‘)
The partial link is a supplement to the link method, assuming that the link has a very long name, "I'm a long, long, long link":
find_element_by_partial_link_text(‘很长的‘)
Or
find_element_by_partial_link_text(‘链接‘)
2.6 Positioning with XPath
XPath positioning is the simplest and most convenient way to do this, and each element has its own location on the Web page, and it has its own location in the code. and XPath represents its absolute path in the code, that is to say, by an absolute address, this element must be found.
At the beginning of my own script when the silly parsing elements of the XPath until you know the Firefox super plugin, Firebug. For https://addons.mozilla.org/zh-CN/firefox/addon/firebug/.
Still take Baidu search box as an example
Get element XPath
Right-click through the firebug to see the element, find the corresponding element of the search box, right-click to copy its XPath, locate the code for
find_element_by_xpath(‘/html/body/div[2]/div[1]/div/div[1]/div/form/span[1]/input‘)
Although XPath is a very simple method, it cannot be abused. Like Baidu Home code basic will not update, itself also belongs to static Web page, element absolute path basically will not change. If you encounter a dynamic Web page or need a flexible positioning element, you can try a relative path, that is, the combination of element attributes
find_element_by_xpath("//input[@id=‘kw‘]") //其中//表示在某个路径下,一个tag name为input,id为kw的元素
Combining hierarchies with attributes
If an address is "Hangzhou City West L. District Wen ER Road West Lake Science and Technology Building", then we can also say "West L. District wen er lu No. 391", this is the combination of hierarchy and attribute
find_element_by_xpath("//span[@class=‘bg s_ipt_wr‘]/input")//span[@class=‘bg s_ipt_wr‘]通过class定位到父
logical operators
find_element_by_xpath("//input[@id=‘kw‘ and @class=‘su‘]/span/input")//用and来连接属性定位元素
2.7 Positioning via CSS
CSS is a language used to describe the presentation of HTML and XML documents, which can be bound to the properties of a page element through a CSS selector, with syntax
find_element_by_css_selector("")
The commonly used selectors are
". Class"
"#id"
"*"
"Element"
"Element>element"
"Element+element"
"[Attribute=value]"
Look more abstract, do not use CSS positioning, it is necessary to say when we give examples, you can refer to the example of W3cschool.
2.8 Through by positioning elements
In fact, Webdriver is another way of writing, passed on to SELENIUM1? Feel no use, the author also did not delve into.
find_element(By.ID,"kw")...
Selenium+python Automation Test Combat (2) Element positioning