html中的elestr作用,selenium 笔记10:webElement的属性&get_attribute()

# -*- coding:utf-8 -*-

"""

@author:百草Lily

@file:selenium_0430.py

@time:2021/4/30

"""

import time

from selenium.webdriver import Chrome

from selenium.webdriver.common.by import By

driver = Chrome()

driver.maximize_window()

driver.get("https://www.baidu.com/")

"""

场景:获取测试对象的属性能够帮我们更好的进行对象的定位。比如页面上有很多class都是'btn'的div,而我们需要定位其中1个有具有title属性的div

方法:由于selenium-webdriver是不支持直接使用title来定位对象的,

所以我们只能先把所有class是btn的div都找到,然后遍历这些div,获取这些div的title属性,一旦发现具体title属性的div,那么返回这个div既可

1. webElement 的属性

"""

ele = driver.find_element(By.ID, "kw")

ele2 = driver.find_element(By.ID, "su")

ele3 = driver.find_element(By.CLASS_NAME, "s_btn_wr")

print(ele)

#

print(ele.id) # eb7f52f6-fbaf-464c-afaa-f60962295f04

print(ele2.location) # {'x': 851, 'y': 188}

print(ele.location)

# dict类型,相对浏览器左上角而言,{'x': 298, 'y': 188}

print(ele.location_once_scrolled_into_view)

# 返回dict类型,{'x': 298, 'y': 102};滚动到元素可见;返回屏幕左上角的位置

print(ele.parent)

#

print(ele.rect)

# dict类型,元素大小和在浏览器中的位置:{'height': 44, 'width': 548, 'x': 298, 'y': 188.39583587646484}

print(ele.screenshot_as_base64)

print(ele.screenshot_as_png)

print(ele.size)

# {'height': 44, 'width': 548}

print(ele.tag_name)

# 返回标签名,str类型,'input'

print(ele.text)

# 文本信息,str类型

#

百度热榜

# 返回:百度热榜,即text获取的时abc标签中间的文本

"""

2.

ele.get_attribute(name)

Gets the given attribute or property of the element.

This method will first try to return the value of a property with the

given name. # 1. dom对象属性

If a property with that name doesn't exist, it returns the

value of the attribute with the same name. # 2. 标签属性值

If there's no attribute with that name, ``None`` is returned. # 3. 都没有返回None

Values which are considered truthy, that is equals "true" or "false",

are returned as booleans. All other non-``None`` values are returned

as strings. For attributes or properties which do not exist, ``None``

is returned.

:Args:

- name - Name of the attribute/property to retrieve.

Example::

# Check if the "active" CSS class is applied to an element.

is_active = "active" in target_element.get_attribute("class")

"""

# 获取元素属性

print(ele.get_attribute("id") ) # kw

print(ele.get_property("id") ) # kw

# print(ele2.get_attribute("location")) # 不存在时,返回None

# print()

# 获取输入框中输入的值(name="value")

ele.send_keys("selenium")

print(ele.get_attribute("value") ) # selenium

print(ele.get_property("value") ) # selenium

# 2.1 name="textContent",表示标签<>XXX>内的文本内容XXX

print(ele.get_attribute("textContent")) # 返回 “”

print(ele3.get_attribute("textContent")) # 返回 “”

# 2.2 name="innerHTML",表示标签内的所有内容,包括html和文本信息

print(ele.get_attribute("innerHTML")) # 返回“”

print(ele3.get_attribute("innerHTML")) # 返回

# 2.3 name="outerHTML",表示包括当前所定位元素在内的

print(ele.get_attribute("outerHTML"))

# 上行输出:

print(ele3.get_attribute("outerHTML"))

# 上行输出:

# 注:有待进一步加深

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值