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'))
相关推荐
太岁又沐风1 小时前
复现并修掉ART hook框架 Pine 调用原方法时的偶发 SIGSEGV
爬虫
黄忠5 小时前
01-系统架构设计-LangGraph状态机与多源异构RAG
python
zzzzzz3105 小时前
假如我是掘金管理员,我先给评论区装个'代码审查'系统
python·程序员·机器人
砍材农夫5 小时前
python环境|conda安装和使用(2)
后端·python
程序员龙叔18 小时前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
用户83562907805121 小时前
使用 Python 操作 Word 内容控件
后端·python
码云骑士1 天前
32-慢查询排查全流程(下)-索引优化实战与最左前缀原则
python
隔窗听雨眠1 天前
大模型加爬虫上篇:技术融合与架构革新
爬虫·架构
闵孚龙1 天前
《PyTorch 深度修炼》Dataset 和 DataLoader:数据如何喂给模型
人工智能·pytorch·python