"Selenium learning Note one" Python + Selenium the way to locate page elements.

Source: Internet
Author: User
Tags xpath continuous integration tools

1, what is selenium, why Web test, everybody use it?

Selenium is designed to re-develop the acceptance test for Web projects. The kernel is written in JavaScript language and is almost supported so that it can run JavaScript browsers and support various platforms such as Windows\linux\macos.

Selenium scripts are written in one of several supported programming languages-Java, Ruby, and Python drivers are currently available. These scripts run in a separate process outside the browser. The task of the driver is to execute the test script and to drive the browser by communicating with the browser bot running in the browser. The communication between the driver and the browser bot uses a simple Selenium-specific connection language Selenese.

Selenium is another useful and important tool in the toolbox of software engineers, designers, and testers. By combining this tool with continuous integration tools, teams can automate acceptance testing and build better software because they can find bugs more easily, earlier, and more frequently. Another advantage of Selenium is that it saves time so that developers and testers do not have to spend time on manual tasks that could (and should) be automated, so that the team can focus on more valuable activities.

2. Several common ways to find (locate) Web elements in selenium.

Python:

1) Find the ID of the element.

From selenium import Webdriver

Browser = Webdriver. Firefox ()

browser.find_element_by_id ("element ' s ID")

2) Locate by the name of the element.

Browser.find_element_by_name ("element ' s name")

3) classname by the applied style sheet name.

Browser.find_element_by_class_name ("Class name")

4) apply features via CSS.

Find_element_by_css_selector ("a[id= ' id ']")

5) through the linked text.

Browser.find_element_link_name ("link name")

6) positioning via XPath.

Browser.find_element_by_xpath ("//***[*= ' * * ']")

Here is a better-written brother's blog post:

  

Selenium the operation of the elements of the Web first must first locate the element, the method of locating the element mainly has the following kinds:

    • Locating elements by ID: find_element_by_id ("Id_vaule")
    • Locating elements by name: Find_element_by_name ("Name_vaule")
    • Positioning elements via tag_name: Find_element_by_tag_name ("Tag_name_vaule")
    • Positioning elements via class_name: Find_element_by_class_name ("Class_name")
    • Positioning elements via CSS: Find_element_by_css_selector (); positioning with CSS is more flexible
    • Positioning elements via XPath: Find_element_by_xpath ("XPath")
    • Positioning via link : Find_element_by_link_text ("Text_vaule") or Find_element_by_partial_link_text ()

Baidu Homepage As an example: The following is the Baidu input box HTML code, can be through Firebug or Google's review elements or to be

<input type= "text" name= "WD" id= "KW1" maxlength= "" style= "WIDTH:474PX;"

Autocomplete= "Off" >

1. Through the ID location, the Baidu input box can be expressed as: find_element_by_id ("KW1")

2. The location by name can be expressed as: Find_element_by_name ("WD")

3. Through tag_name positioning: input is actually tag_name (sign), also can be expressed as:

Find_element_by_tag_name ("input")

Here is the "Baidu button" html

<span class= "BTN_WR" >

<inputtype= "Submit" value= "Baidu a Bit" id= "SU1" class= "BTN" onmousedown=

"This.classname= ' Btnbtn_h '" onmouseout= "this.classname= ' btn '" >

</span>

4. Through the class_name positioning, "Baidu a bit" button can be expressed as find_element_by_class_name ("BTN_WR")

5. Through CSS positioning, this is more flexible, want to fully understand, the time spent is

More, the individual felt no need

Baidu input Box

<input

Type= "text" name= "WD" id= "KW1" maxlength= "the style=" WIDTH:474PX; " Autocomplete= "Off" >

    • If you take the ID, the Baidu input box can be expressed as: Find_element_by_css_selector ("a[id=\" kw1\ "]")
    • If name is taken, it can be represented as: Find_element_by_css_selector ("a[name=\" wd\ "]")
    • <aonclick= "Querytab (this);" mon= "col=502&pn=0" title= "web" href= "http://www.baidu.com/" > Web </a>
    • can also use the title, such as Baidu's web links can be expressed as find_element_by_css_selector ("a[title=\" Web\ "]")

<aclass= "Recyclebinxz" href= "javascript:void (0);" >

    • Also can use class, the above code can be used Find_element_by_css_selector ("A.recyclebin")

6. Positioning by XPath

First we want to understand that XPath is the above thing, XPath is a kind of XML

The language of the positioned element in the document. Because HTML can be seen as an implementation of XML,

So selenium users use this powerful language in Web applications to set

Bit elements, please read the details:

What is xpath:http://www.w3.org/tr/xpath/
XPath Basics Tutorial: http://www.w3schools.com/xpath/default.asp

Selenium of the Misunderstood XPath:

http://magustest.com/blog/category/webdriver/

Xpath:attributer (attributes)

Driver.find_element_by_xpath ("//input[@id = ' kw1 ']")

An element that represents the ID =KW1 under the input label

Xpath:idrelative (ID dependency)

Driver.find_element_by_xpath ("//div[@id = ' fm ']/form/span/input")

An element that represents a id=fm with a div tag under the/form/span/input level tab

Driver.find_element_by_xpath ("//tr[@id = ' check ']/td[2]")

A TR that represents the id ' check ', and the 2nd TD in his

Xpath:position (location)

Driver.find_element_by_xpath ("//input")

Driver.find_element_by_xpath ("//tr[7]/td[2]")

Represents the 2nd TD in the 7th TR

Xpath:href (horizontal reference)

Driver.find_element_by_xpath ("//a[contains (Text (), ' Web page ')]")

Indicates that there is an element under the A tag (text) containing (contains) ' Web page '

Xpath:link

Driver.find_element_by_xpath ("//a[@href = ' http://www.baidu.com/']")

There is a tag called a, he has a link href= ' http://www.baidu.com/element

7. Navigate through Link

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

#coding =utf-8

Fromseleniumimportwebdriver

Importtime

DF = Webdriver. Firefox () #选择firefox浏览器

Df.get ("http://www.baidu.com") #打开百度网页

Time.sleep (2) #暂停2秒, not milliseconds

Df.find_element_by_link_text ("Paste"). Click () #点击贴吧链接

Time.sleep (2)

Df.quit () #关闭浏览器

Partiallinktext Positioning

Through part of the link location, this is sometimes used, I have not thought of good use. With the above example, I can match only a subset of the text in the link:

Browser.find_element_by_partial_link_text ("Paste"). Click ()

Through the Find_element_by_partial_link_text () function, I only used the "paste" word, the script found the "Paste" link

"Selenium learning Note one" Python + Selenium the way to locate page elements.

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.