Selenium2+python automation 35 - get element attributes

foreword

Usually, before making assertions, the attributes of elements on the interface must be obtained first, and then compared with the expected results. This article introduces several common ways to get element attributes.

1. Get the page title

1. There are many friends who don't know where the title is, look at the upper left corner of the picture below.

2. The method of getting the title is very simple, you can get it directly from driver.title

2. Get the text of the element

1. The text information displayed on the page as shown in the figure below can be directly obtained

2. View element attributes: < a id=" setf" target=" _blank" onmousedown=" return ns_c({'fm':'behs','tab':'favorites','pos':0})   

" href=" //www.baidu.com/cache/sethelp/help.html " > Set Baidu as the homepage </a> 

3. Get the text through driver.text

3. Get the label of the element

1. Get the label attribute of the Baidu input box

Fourth, get other attributes of the element

1. Get other attribute methods: get_attribute("attribute"), the parameters here can be any attributes such as class, name, etc.

2. For example, to obtain the class attribute of the Baidu input box

5. Get the text value in the input box

1. If you enter content in the Baidu input box, the content of the input box here can also be obtained

6. Get the browser name

1. It is very simple to get the browser name, you can get it with driver.name

(Code reference point 7)

7. 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
# 获取元素的标签
tag = driver.find_element_by_id("kw").tag_name
print tag
# 获取元素的其它属性
name = driver.find_element_by_id("kw").get_attribute("class")
print name
# 获取输入框的内容
driver.find_element_by_id("kw").send_keys("yoyoketang")
value = driver.find_element_by_id("kw").get_attribute("value") print driver.name# Get the browser name
print value

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348185&siteId=291194637