018、元素属性获取 (text、tag_name、get_attribute)

 

一、元素属性获取

  .text :获取元素的text属性值
  .tag_name:  获取元素标签名称
  .get_attribute() :获取属性对应的值

 

 

1、.text :获取元素的 text 属性值  和  .tag_name:  获取元素标签名称

示例代码如下:

# -*- coding:utf-8 -*-
# Author:  Sky
# Email:   2780619724@qq.com
# Time:    2021/8/18 12:25
# Project: day01
# Module:  study_20.py
# Environment: Python3.8.6 , Selenium3 环境 ( 3.141.0 版本)
# Environment: Chrome ( 92.0.4515.131, 正式版本) + chromedriver(92.0.4515.107版本)

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get("http://49.235.92.12:8200/users/login/")
time.sleep(3)

# 定位回到首页text
t1 = driver.find_element_by_class_name("index-font").text
print(t1)

# 电话号码text
t2 = driver.find_element_by_xpath("/html/body/header/div/div/ul/li[1]/span").text
print(t2)

# tag_name
tag = driver.find_element_by_id("username").tag_name
print(tag)

driver.quit()

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Web_AutoTest\Temp\day01\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Web_AutoTest/Temp/day01/ch_002/study_20.py
回到首页
8888888
input

Process finished with exit code 0

 

2、定位父元素,可以获取所有子孙元素文本

示例代码如下:

# -*- coding:utf-8 -*-
# Author:  Sky
# Email:   2780619724@qq.com
# Time:    2021/8/18 12:25
# Project: day01
# Module:  study_20.py
# Environment: Python3.8.6 , Selenium3 环境 ( 3.141.0 版本)
# Environment: Chrome ( 92.0.4515.131, 正式版本) + chromedriver(92.0.4515.107版本)

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get("http://49.235.92.12:8200/users/login/")
driver.maximize_window()
time.sleep(3)

# 获取页面上的服务电话
t2 = driver.find_element_by_xpath("/html/body/header/div/div/ul/li[1]/span").text
print(t2)

# 获取所有子孙元素文本
# 服务电话:8888888
t3 = driver.find_element_by_xpath("/html/body/header/div/div/ul/li[1]").text
print(t3)

driver.quit()

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Web_AutoTest\Temp\day01\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Web_AutoTest/Temp/day01/ch_002/study_20.py
8888888
服务电话:8888888

Process finished with exit code 0

 

3、获取这个登陆按钮元素的每个属性对应的值

  <input class="btn btn-green" id="jsLoginBtn" type="submit" value="立即登录 > ">

示例代码如下:

# -*- coding:utf-8 -*-
# Author:  Sky
# Email:   2780619724@qq.com
# Time:    2021/8/18 12:25
# Project: day01
# Module:  study_20.py
# Environment: Python3.8.6 , Selenium3 环境 ( 3.141.0 版本)
# Environment: Chrome ( 92.0.4515.131, 正式版本) + chromedriver(92.0.4515.107版本)

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get("http://49.235.92.12:8200/users/login/")
driver.maximize_window()
time.sleep(3)

btn = driver.find_element_by_id("jsLoginBtn")
btn_value = btn.get_attribute("value")
print(btn_value)
btn_class = btn.get_attribute("class")
print(btn_class)
btn_type = btn.get_attribute("type")
print(btn_type)

driver.quit()

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Web_AutoTest\Temp\day01\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Web_AutoTest/Temp/day01/ch_002/study_20.py
立即登录 > 
btn btn-green
submit

Process finished with exit code 0

 

4、如何获取输入框的值?

示例代码如下:

# -*- coding:utf-8 -*-
# Author:  Sky
# Email:   2780619724@qq.com
# Time:    2021/8/18 12:25
# Project: day01
# Module:  study_20.py
# Environment: Python3.8.6 , Selenium3 环境 ( 3.141.0 版本)
# Environment: Chrome ( 92.0.4515.131, 正式版本) + chromedriver(92.0.4515.107版本)


from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://49.235.92.12:8200/users/login/")

# 没输入之前,输入框的值
t1 = driver.find_element_by_id("username").get_attribute("value")
print(t1)

# 输入文本
driver.find_element_by_id("username").send_keys("1234@qq.com")

# 我如何知道刚才输入的内容是成功的
t2 = driver.find_element_by_id("username").get_attribute("value")
print(t2)

driver.quit()

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Web_AutoTest\Temp\day01\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Web_AutoTest/Temp/day01/ch_002/study_20.py

1234@qq.com

Process finished with exit code 0

 

5、.get_attribute() 如果没有这个属性,不会报错, 返回的是None

示例代码如下:

# -*- coding:utf-8 -*-
# Author:  Sky
# Email:   2780619724@qq.com
# Time:    2021/8/18 12:25
# Project: day01
# Module:  study_20.py
# Environment: Python3.8.6 , Selenium3 环境 ( 3.141.0 版本)
# Environment: Chrome ( 92.0.4515.131, 正式版本) + chromedriver(92.0.4515.107版本)


from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://49.235.92.12:8200/users/login/")

# 没输入之前,输入框的值
t1 = driver.find_element_by_id("username").get_attribute("valuexxx")
print(t1)

driver.quit()

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Web_AutoTest\Temp\day01\venv\Scripts\python.exe D:/SkyWorkSpace/WorkSpace/Web_AutoTest/Temp/day01/ch_002/study_20.py
None

Process finished with exit code 0

 

posted @ 2021-08-18 14:57  空-山-新-雨  阅读(513)  评论(0编辑  收藏  举报