Python Selenium element Positioning (iii)

Source: Internet
Author: User

Two blog post introduced the Python selenium environment to build and write the first automated test script, from the second example to do UI-level automation testing, there is a crucial factor, that is, the element positioning, only from the page to find this element, We can manipulate this element, so let's go down and see how to position the element.

Selenium provides 8 elements of the method of positioning (we have to learn the positioning of elements, first of all can learn the basics of the front-end, so that we can learn the automation test, we may see: http://www.runoob.com/)

    • find_element_by_id
    • Find_element_by_name
    • Find_element_by_xpath
    • Find_element_by_link_text
    • Find_element_by_partial_link_text
    • Find_element_by_tag_name
    • Find_element_by_class_name
    • Find_element_by_css_selector

Here we describe in detail the meaning of each method and the use of each method.

1.FIND_ELEMENT_BY_ID location based on tag ID

Sample HTML code:

<HTML> <Body>   <inputID= "KW"name= "username"type= "text" />   <inputname= "Password"type= "Password" />   <inputname= "Continue"type= "Submit"value= "Login" /> </Body><HTML>

Web pages need to open the browser by the developer tool, press F12 to get the page elements, we have seen the above page HTML code, we now want to find id= "kw" element

driver.find_element_by_id ('kw')  # location via ID

2.find_element_by_name based on the label's name

Driver.find_element_by_name (' username')  # location by name

3.find_element_by_xpath based on XPath positioning

Find_element_by_xpath (//*[@id ="kw"])

Here is how to get the Xpath path of the page element, if you are the God can write their own, if the same as the author, can be obtained through the developer tool, select the element right-click->copy->copy XPath, you can directly copy to the XPath path.

4.find_element_by_link_text and Find_element_by_partial_link_text use text links to locate elements, two of them are very similar, and the functions are similarly, but they are all matched and one is the matching part.

To add a paragraph to our previous example code, let's look at how to locate

<HTML> <Body>   <inputID= "KW"name= "username"type= "text" />   <inputname= "Password"type= "Password" />   <inputname= "Continue"type= "Submit"value= "Login" />   <DivID= "U1">   <ahref= "Http://news.baidu.com"name= "Tj_trnews"class= "Mnav">News</a>   <ahref= "Http://www.hao123.com"name= "tj_trhao123"class= "Mnav">Hao123</a>   <ahref= "Http://map.baidu.com"name= "Tj_trmap"class= "Mnav">Map</a>   <ahref= "Http://v.baidu.com"name= "Tj_trvideo"class= "Mnav">Video</a>   <ahref= "Http://tieba.baidu.com"name= "Tj_trtieba"class= "Mnav">Stick</a>   <ahref= "Http://xueshu.baidu.com"name= "Tj_trxueshu"class= "Mnav">Academic</a> </Body><HTML>

So here's a couple of ways to locate this element of the news.

through to Find_element_by_link_text positioning

# -*-coding:utf-8-*- Import  Time  from Import Webdriverdriver   =  webdriver. Chrome () Driver.maximize_window () driver.get ("file:///C:/Users/hunk/Desktop/id.html " ) Driver.find_element_by_link_text (' news '). Click () Time.sleep ( 3) driver.quit ()   # Exit Browser

Positioning effect:

through to Find_element_by_partial_link_text positioning

5.find_element_by_tag_name based on the name of the label, this method is not used, because the label of a page in the name of the repetition too early, it is too difficult to locate.

Driver.find_element_by_tag_name ("input")

6.find_element_by_class_name is positioned by class name

 <  html  >  <  body   >  <  p  class  = "content"  >  Site content goes here. </ p  >  </ body  >  <  html  >  
Driver.find_element_by_class_name (' content')  # Location by class name

7.find_element_by_css_selector based on the attributes of the element to locate, this method in the actual process is more practical, and very simple, here we first look at the syntax, here is a key point is that in this method of positioning can write regular expressions to locate elements, Then write an instance to locate, example we use Baidu website to locate, then search selenium keyword.

< HTML > < Body >  <  class= "Content">Site content goes here. </ P > </ Body > < HTML >

Positioning syntax

Driver.find_element_by_css_selector ("p[class=\" content\ "]")  #  Based on element properties

Example:

#-*-coding:utf-8-*-Import Time fromSeleniumImportWebdriverdriver=Webdriver. Chrome () Driver.maximize_window () Driver.get ("http://www.baidu.com") Driver.find_element_by_css_selector ("input[id=\ "kw\"]"). Send_keys ('Selenium')#positioning input Box input SeleniumDriver.find_element_by_css_selector ("input[type=\ "submit\"]"). Click ()#Locate the Search button, click the button, select the typeTime.sleep (5) Driver.quit ()#Exit Browser

Take a look at the animated effect.

In fact, the location of the element is very simple, but the actual process of positioning the HTML page there are some special places, as long as we disassemble the analysis, nature will not pour us.

The following methods return the result is a list, as a single element is positioned, but the results returned are different, this is not described in detail.

    • find_elements_by_id
    • Find_elements_by_name
    • Find_elements_by_xpath
    • Find_elements_by_link_text
    • Find_elements_by_partial_link_text
    • Find_elements_by_tag_name
    • Find_elements_by_class_name
    • Find_elements_by_css_selector

Python Selenium element Positioning (iii)

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.