Easy Automation---selenium-webdriver (python) (iii)

Source: Internet
Author: User
Tags tag name xpath

Original URL:

Http://www.cnblogs.com/fnng/p/3183777.html


This section focuses on: positioning of Simple objects

-----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.

But, touching a set of control properties below, we cried.

<th width= ></th>
<th width= "" > File name </th>
<th class= "C1" > Creation time </th>
<th class= "C1" > Status </th>
<th class= "C1" > File size </th>
<th class= "C1" > Long </ Th>

The following CSS and XPath are not as intuitive as above, and may not be very well understood if you don't understand the front end.

CSS Positioning

CSS (cascading Style Sheets) is a language that is used to describe the performance of HTML and XML documents. CSS uses selectors to bind attributes to page elements. These selectors can be used by selenium as additional positioning strategies.

CSS is more flexible to select any property of the control, in the example above:

Find_element_by_css_selector ("#kw")

Through the Find_element_by_css_selector () function, select the Baidu input box ID attribute to define

You can also take the Name property

<a href= "http://news.baidu.com" name= "Tj_news" > News </a>

Driver.find_element_by_css_selector ("a[name=\" tj_news\ "]"). Click ()

You can take the title property

<a onclick= "Querytab (this);" mon= "col=502&pn=0" title= "web" href= "http://www.baidu.com/" > Web page </a>

Driver.find_element_by_css_selector ("a[title=\" Web\ "]"). Click ()

Can also be taken from ...:

<a class= "RecycleBin xz" href= "javascript:void (0);" >

Driver.find_element_by_css_selector ("A.recyclebin"). Click ()

Although I do not fully understand the positioning of CSS, but it should look like a very flexible and cow x 

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.