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'))
相关推荐
ragnwang2 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
王中阳Go2 小时前
后端转 Agent 岗面试怎么答:8 个高频问题与应答口径
人工智能·后端·python·大语言模型
weixin_307779133 小时前
RAG 面试陷阱——混合检索并非无条件优越:一个条件反转实验
人工智能·python·面试·职场和发展
云和数据.ChenGuang4 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain
荣码6 小时前
长上下文与大文档处理:200页PDF怎么喂给AI,我踩了4个坑
java·python
databook7 小时前
Pandas 与 Polars:语法、性能和资源占用方面的对比
python·数据分析·pandas
威哥爱编程7 小时前
Python 智能体开发实战 10:本地跑得好好的,一并发就崩?把它做成一个能上线的服务
python·agent
威哥爱编程7 小时前
Python 智能体开发实战 09:改了提示词,到底是变好还是变坏?搭一套能打分的离线评测
python·agent
meiying88388 小时前
多快递公司快递单号批量查询接口怎么对接?Python 聚合查询实现
java·大数据·python
Hello_Pyhx8 小时前
Deep-Live-Cam 源码配置思路:环境、模型与执行后端分开排查
python