栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

第四节 Selenium WebElement属性

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第四节 Selenium WebElement属性

当我们使用WebDriver的find方法定位元素后,会返回一个WebElement对象,该对象用来描述web页面上的一个元素,以下是WebElement的常用属性和方法。

WebElement常用属性如下:

属性描述
id标识
size宽高
rect宽高和坐标
tag_name标签名称
text文本内容
from selenium import webdriver
from time import sleep

class testCase(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://sahitest.com/demo/linkTest.htm')

    def test_webelment_prop(self):
        e = self.driver.find_element_by_id('t1')
        print(type(e))
        print(e.id)
        print(e.tag_name)
        print(e.size)
        print(e.rect)
        print(e.text)

if __name__ == '__main__':
    case = testCase()
    case.test_webelment_prop()

执行结果

"D:test toolpythonProjectspythonStadyvenvscriptspython.exe" "D:/test tool/pythonProjects/pythonStady/selenium/seleniumDemo3.py"

beeb186e-4d3e-4915-a6bd-1d21f98ad0f7
input
{'height': 21, 'width': 737}
{'height': 21, 'width': 737, 'x': 8, 'y': 542.90625}


Process finished with exit code 0

WebElement常用属性如下:

方法描述
send_key()输入内容
clear()清空内容
click()单击
get_attribute()获得属性值
is_selected()是否被选中
is_enabled()是否可用
is_displayed()是否显示
value_of_css_propertycss属性值
from selenium import webdriver
from time import sleep

class testCase(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://sahitest.com/demo/linkTest.htm')


    def test_webelment_method(self):
        e = self.driver.find_element_by_id('t1')
        e.send_keys('hello word')

        sleep(2)

        print(e.get_attribute('type'))
        print(e.get_attribute('name'))
        print(e.get_attribute('value'))

        print(e.value_of_css_property('font'))
        print(e.value_of_css_property('color'))

        sleep(2)
        e.clear()

if __name__ == '__main__':
    case = testCase()
    case.__init__()
    # case.test_webelment_prop()
    case.test_webelment_method()
    
"D:test toolpythonProjectspythonStadyvenvscriptspython.exe" "D:/test tool/pythonProjects/pythonStady/selenium/seleniumDemo3.py"
text

hello word
13.3333px Arial
rgba(0, 0, 0, 1)

Process finished with exit code 0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/461735.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号