Selenium2+python Automation 35-Get element properties

Source: Internet
Author: User
Tags setf

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

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.