Python selenium获取元素信息

视频版教程:一天掌握python爬虫【基础篇】 涵盖 requests、beautifulsoup、selenium

主要text属性和三个方法get_attribute(),get_property(),get_dom_attribute()

text属性获取元素的文本信息;

get_attribute(),get_property(),get_dom_attribute()三个方法都是获取元素的属性值

区别是property是DOM中的属性,是JavaScript里的对象;

attribute是HTML标签上的属性,

dom_attribute则只能是HTML标签规范定义的属性(自定义的属性获取不到)。

我们一般开发用的是get_attribute()方法。

python 复制代码
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Chrome()
url = "http://www.java1234.com/"

browser.get(url)

time.sleep(2)

input = browser.find_element(By.ID, "time")

print(input.text)
print(input.get_attribute('id'))
print(input.get_property('id'))
print(input.get_dom_attribute('id'))
print(input.get_attribute('class'))
print(input.get_property('class'))
print(input.get_dom_attribute('class'))
相关推荐
AIFQuant12 分钟前
如何利用免费股票 API 构建量化交易策略:实战分享
开发语言·python·websocket·金融·restful
布局呆星21 分钟前
SQLite数据库的介绍与使用
数据库·python
2401_8384725121 分钟前
用Python和Twilio构建短信通知系统
jvm·数据库·python
weixin_4521595529 分钟前
如何从Python初学者进阶为专家?
jvm·数据库·python
Hello.Reader30 分钟前
面向 403 与域名频繁变更的合规爬虫工程实践以 Libvio 系站点为例
爬虫·python·网络爬虫
m0_6632340142 分钟前
Libvio.link爬虫技术与反爬攻防解析
爬虫
深蓝海拓44 分钟前
PySide6从0开始学习的笔记(二十五) Qt窗口对象的生命周期和及时销毁
笔记·python·qt·学习·pyqt
Dfreedom.1 小时前
开运算与闭运算:图像形态学中的“清道夫”与“修复匠”
图像处理·python·opencv·开运算·闭运算
2301_790300961 小时前
用Python读取和处理NASA公开API数据
jvm·数据库·python
葱明撅腚1 小时前
利用Python挖掘城市数据
python·算法·gis·聚类