Objective
Usually before making an assertion, you first get the attributes of the elements on the interface and then compare them with the expected results. This article describes several common ways to get element attributes.
First, get the page title
1. A lot of small partners do not know where the title is long, look at the upper left corner.
2. Getting the title method is simple, and driver.title can get it directly
Second, get the text of the element
1. The text information displayed on the page can be obtained directly from the
2. View element properties: <a id= " SETF " target= "_blank" onmousedown= " return Ns_c ({' fm ': ' Behs ', ' tab ': ' Favorites ', ' pos ': 0})
" href="//www.baidu.com/cache/sethelp/help.html "> Baidu homepage </a>
3. Get to Text via Driver.text
Third, get the label of the element
1. Get the label properties of the Baidu input box
Iv. Obtaining additional attributes of an element
1. Get other property methods: Get_attribute ("attribute"), where the argument can be any property of class, name, etc.
2. If you get the class attribute of the Baidu input box
V. Get the text value in the input box
1, if in the Baidu input box input content, here the input box content also can obtain to
Vi. getting the name of the browser
1. Get the browser name is simple, with driver.name can get the
(Code reference 7th)
Seven, reference code
# Coding:utf-8
From selenium import Webdriver
Import time
Driver = Webdriver. Firefox ()
Driver.implicitly_wait (10)
Driver.get ("http://www.baidu.com")
Time.sleep (2)
title = Driver.title
Print title
Text = driver.find_element_by_id ("SETF"). Text
Print text
# Gets the label of the element
Tag = driver.find_element_by_id ("kw"). tag_name
Print tag
# Get other attributes of an element
Name = driver.find_element_by_id ("kw"). Get_attribute ("class")
Print Name
# Get the contents of the input box
driver.find_element_by_id ("kw"). Send_keys ("Yoyoketang")
Value = driver.find_element_by_id ("kw"). Get_attribute ("value")
Print value
# Get Browser name
Print Driver.name
Selenium2+python Automation 35-Get element properties