Python+selenium: Browser Webdriver Action (1)--Basic object positioning

Source: Internet
Author: User
Tags tag name xpath

1. Positioning of simple objects-----The core of automated testing

Object positioning should be the core of automated testing, to manipulate an object, you should first identify this object. An object is like a person, he will have a variety of characteristics (attributes), such as more than we can through a person's ID number, name, or he lives in which street, floor, number to find this person. Then an object has a similar property, and we can find this object through this property.

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

3. Take the attribute information of the Baidu input box as an example, you can capture the input box in a series of ways

#Coding=utf-8 fromSeleniumImportWebdriverImportTimebrowser=Webdriver. Firefox () Browser.get ("http://www.baidu.com") Time.sleep (2)######## #百度输入框的定位方式 ###########location by ID methodBROWSER.FIND_ELEMENT_BY_ID ("kw"). Send_keys ("Selenium")#Locate by name modeBrowser.find_element_by_name ("WD"). Send_keys ("Selenium")#location by tag name ModeBrowser.find_element_by_tag_name ("input"). Send_keys ("Selenium")#position by class nameBrowser.find_element_by_class_name ("S_ipt"). Send_keys ("Selenium")#Navigate by CSS modeBrowser.find_element_by_css_selector ("#kw"). Send_keys ("Selenium")#positioning by Xphan WayBrowser.find_element_by_xpath ("//input[@id = ' kw ')"). Send_keys ("Selenium")############################################browser.find_element_by_id ("su"). Click () time.sleep (3) Browser.quit ()

4. Introduce respectively

Baidu input Box properties such as (mouse in the input box---right-click---check)

(1) Id/name

From the Id,name property you can see, you can get it through a function:

id= "KW"

by find_element_by_id ("kw") function is to capture to the Baidu input box

Name= "WD"

Baidu input box can also be captured by Find_element_by_name ("WD") function

(2) class Name/tag name

You can see that there are not only id,name properties, but also Class (s_ipt), tag name (--input) Properties

Class= "S_ipt"

Capture the Baidu input box via the Find_element_by_class_name ("S_ipt") function.

<input>

Input is the name of the tag and can be located by the Find_element_by_tag_name ("input") function.

(3) Link text/partial link text

Sometimes not an input box is not a button, but a text link, we can

For example, Baidu homepage in the upper right corner of the bar link, you can get through the link method

Link text

by Find_element_by_link_test ("Paste"). Click () function to locate

Partial link text: Part of the location, only with the "paste" word, the same can find "paste" link

by Find_element_by_link_test ("Paste"). Click () function to locate

(4) XPath

XPath is a language that locates elements in an XML document. Because HTML can be seen as an implementation of XML, selenium users use this powerful language to locate elements in a Web application.

XPath extends the above ID and name positioning methods, providing a number of possibilities, such as the following applications:

Xpath:attributer (attribute) Driver.find_element_by_xpath ("//input[@id = ' kw ')"). Send_keys ("Selenium")#element of ID =kw under input tagxpath:idrelative (id dependency) Driver.find_element_by_xpath ("//div[@id = ' FM ']/form/span/input"). Send_keys ("Selenium")#The id=fm element with a div tag under the/form/span/input Level tabDriver.find_element_by_xpath ("//tr[@id = ' check ']/td[2]"). Click ()#tr with id ' check ', locate the 2nd TD in hisxpath:position (position) Driver.find_element_by_xpath ("//input"). Send_keys ("Selenium") Driver.find_element_by_xpath ("//tr[7]/td[2]"). Click ()#7th TR inside the 2nd TDXpath:href (horizontal reference) Driver.find_element_by_xpath ("//a[contains (text (), ' Web page ')]"). Click ()#under the A tag there is an element of text containing (contains) ' Web page 'Xpath:linkdriver.find_element_by_xpath ("//a[@href = ' http://www.baidu.com/')"). Click ()#there's a tag called a, he has a link href= ' http://www.baidu.com/element

(5) CSS

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

Flexible CSS allows you to select any property of the control

Take the id attribute:

such as attribute information: <input id= "kw" class= "S_ipt" type= "text" maxlength= "" "Name=" WD "autocomplete=" Off ">

Position by Find_element_by_css_selector ("#kw") function

Take the Name property:

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

by Find_element_by_css_selector ("a[name=\" tj_news\ "]"). Click () function to locate

Take the Title property:

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

by Find_element_by_css_selector ("a[title=\" Web\ "]"). Click () function to locate

Python+selenium: Browser webdriver Action (1)--basic object positioning

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.