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'))
相关推荐
小凯123457 分钟前
pytest框架-详解(学习pytest框架这一篇就够了)
python·学习·pytest
逻极7 分钟前
pytest 入门指南:Python 测试框架从零到一(2025 实战版)
开发语言·python·pytest
网络安全-老纪9 分钟前
一文2000字手把手教你自动化测试Selenium+pytest+数据驱动
自动化测试·软件测试·selenium·测试工具·pytest
OnYoung13 分钟前
更优雅的测试:Pytest框架入门
jvm·数据库·python
芝士爱知识a1 小时前
【FinTech前沿】重塑衍生品交易:十维深度解析 AlphaGBM 智能期权分析平台
人工智能·python·量化交易·期权分析·alphagbm·期权交易·ai期权
小白菜又菜2 小时前
Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
python·算法·leetcode
Omigeq2 小时前
1.2.2 - 采样搜索算法(以RRT和RRT*为例) - Python运动规划库教程(Python Motion Planning)
开发语言·人工智能·python·机器人
凌云拓界2 小时前
TypeWell全攻略(二):热力图渲染引擎,让键盘发光
前端·后端·python·计算机外设·交互·pyqt·数据可视化