# -*- 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"))
# 上行输出:
# 注:有待进一步加深