An introduction to the positioning of Selenium+python elements

Source: Internet
Author: User
Tags sleep function tag name xpath

Web page Automation Testing the most basic requirement is to locate the individual elements, and then to the elements of the various operations (input, click, clear, submit, etc.), so today to summarize the Selenuim+python the most basic of several positioning methods and examples, to Baidu search input box for example, Use Google Chrome to specify the usage of each positioning method.

1. Webdriver method for importing selenium packages

Input from selenium import Webdriver

2. Introduce the sleep function from the time module and use the Sleep function to hibernate the program

Enter from time import sleep

3. Open the browser

(1) Get browser driver and open Google Chrome, note that the first letter of the browser should be capitalized

Dr=webdriver. Chrome ()

(2) Open the Web page under test

url = "https://www.baidu.com/"

Dr.get (URL)

4. Element Positioning method

(1) locating----find_element_by_id by ID ()

Example: dr.find_element_by_id ("kw"). Send_keys ("Selenium automation") #输入搜索关键字

Note: The ID value must be unique and the ID value is dynamic and cannot be positioned using this method.

(2) Locate----by name Find_element_by_name ()

Example: Dr.find_element_by_name ("WD"). Send_keys ("Selenium automation") #输入搜索关键字

Note: The name value must also be unique.

(3) through class_name positioning----find_element_by_class_name ()

Example: Dr.find_element_by_class_name ("S_ipt"). Send_keys ("Selenium automation") #输入搜索关键字

Note: The value of the class_name must also be unique.

(4) through Link_text positioning----find_element_by_link_text ()

Example: Dr.find_element_by_link_text ("News"). Click () #点击按钮

Note: The value of the Link_text must also be unique.

(5) Partial_link_ text blur text location----Find_element_by_partial_link_text ()

Example: Dr.find_element_by_partial_link_text ("Reform and Opening"). Click () #点击按钮

Note: The value of the Link_text must also be unique.

(6) through tag_name positioning----find_element_by_tag_name ()

Example: Dr.find_element_by_tag_name ("Input"). Send_keys ("Selenium automation") #输入搜索关键字

Tag names are the easiest to repeat, and when you locate a set of data, you can use

Inputboxes = dr.find_elements_by_tag_name ("input")

for inputBox in inputboxes:

If Inputbox.get_attribute ("maxlength") = = "255" and \
Inputbox.get_attribute ("sautocomplete") = = ' off ':
Inputbox.send_keys ("Selenum Automation")

(7) With XPath positioning, XPath is a language for locating elements in an XML document----Find_element_by_xpath ()

(7.1). Position according to Absolute path

path1= "/html/body/div/div/div/div/div/form/span/input"

Dr.find_element_by_xpath (path1). Send_keys ("Selenum Automation") #输入搜索关键字

(7.2). Positioning based on element attributes

Dr.find_element_by_xpath ("//input[@autocomplete = ' off ']"). Send_keys ("Selenum Automation") #输入搜索关键字

(7.3). Positioning based on multiple attribute combinations

Dr.find_element_by_xpath ("//input[@autocomplete = ' off ' and @id = ' kw ']"). Send_keys ("Selenum Automation") #输入搜索关键字

Note: The quotation marks in parentheses cannot be the same as in square brackets.

(7.4). Positioning by combination of hierarchy and attribute

path1 = "//span/input[@class = ' s_ipt ']"

Dr.find_element_by_xpath (path1). Send_keys ("Selenum Automation") #输入搜索关键字

8. Positioning----Find_element_by_css_selector () via CSS

(8.1). Through the class attribute

Dr.find_element_by_css_selector (". ') S_ipt "). Send_keys (" Selenum Automation ") #输入搜索关键字

Note the point:. Indicates that the element is positioned through the class attribute

(8.2). Locating elements by id attribute

Dr.find_element_by_css_selector ("#kw")

Note the point: #号表示通过id属性来定位元素

(8.3). Locating elements by tag name

Dr.find_element_by_css_selector ("input")

(8.4) Positioning elements via attributes (very common)

Dr.find_element_by_css_selector ("[name= ' wd ']")

Dr.find_element_by_css_selector ("[maxlength= ' 255 ']")

Property value contains a value

The property value contains WD: Applies to property values separated by spaces.

Dr.find_element_by_css_selector ("[name~= ' wd ']")

(8.5) Parent-child positioning elements through a hierarchy

Find a tag with a FATHER element named span, and all its tags are called child elements of input

Dr.find_element_by_css_selector ("Span>input")

(8.6) Positioning by combination of hierarchy and attribute

Tag name #id attribute value: Refers to an element with an id attribute of kw under the input tag

Dr.find_element_by_css_selector ("input#kw")

Tag Name. Class Property value: Refers to the element under the input tag with the class attribute S_ipt

Dr.find_element_by_css_selector ("Input.s_ipt")

Tag Name [property = ' property value ']: refers to the element under the input tag named WD

Dr.find_element_by_css_selector ("Input[name= ' wd ']")

Parent element Tag name > label name. Class Property value: Refers to the element under the input tag in span under the class attribute S_ipt

Dr.find_element_by_css_selector ("Span>input.s_ipt")

9. Positioning elements via by

Types of targeting include By.id,by.name,by.class_name,by.tag_name,by.link_text,by.partial_link_text,by.xpath,by.css_selector

Refer to the description above 1-8 for the specific positioning method.

Example: Dr.find_element (by.id, ' kw ')

Note: Using the By method requires importing the by class first.

5. Instance Login QQ Mailbox

#导入selenium包的webdriver方法

     From selenium import Webdriver
#导入By类
From selenium.webdriver.common.by Import by
#引入sleep函数
From time import sleep
#获取浏览器驱动 and open the browser
Dr=webdriver. Chrome ()
Url= "https://mail.qq.com/"
Dr.get (URL)
Sleep (2)
#将浏览器窗口最大化
Dr.maximize_window ()
Sleep (2)
#定位表单位置
frame = dr.find_element_by_id (' Login_frame ')
Dr.switch_to.frame (frame)
dr.find_element_by_id (' Switcher_plogin '). Click ()
#输入用户名密码 and click the login button
dr.find_element_by_id (' u '). Send_keys (' * * * * * @qq. com ')
dr.find_element_by_id (' P '). Send_keys (' password ')
dr.find_element_by_id (' Login_button '). Click ()
Dr.switch_to_default_content ()

An introduction to the positioning of Selenium+python elements

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.