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'))
相关推荐
2601_9548118210 分钟前
AI科学实验室MHS标准解读:AI智能体如何统一控制实验室设备接口
人工智能·python
一位正在转型AI全栈的前端工程师10 分钟前
AI 全栈学习之旅 -Week 9:什么是AI Agent?从Function Calling到LangGraph
前端·python
用户03321266636713 分钟前
使用 Python 为 PowerPoint 设置背景色和背景图【附代码示例】
python
ever_up97328 分钟前
LangChain基础知识概述1
人工智能·python·langchain
深蓝电商API28 分钟前
Redis 在分布式爬虫中的作用
redis·分布式·爬虫
asdzx671 小时前
Python 实现 PDF 文本查找与高亮标注
开发语言·python·pdf
zander2581 小时前
LeetCode 1143. 最长公共子序列
开发语言·python·算法
轩情吖2 小时前
Python基础知识点完整总结
python·字符串·编程语言·函数·列表·元组·字典
青 春 记 忆2 小时前
零基础入门python69:为 FastAPI 项目构建可复现 Docker 镜像
python·fastapi·后端开发
<花开花落>2 小时前
Python 项目迁移到 uv:经验小结与可复用工作流
python·uv