Python Selenium 获取 属性 值

Windows 10 + Python 3.7 + selenium==3.141.0 + urllib3==1.26.2 + Google Chrome 120.0.6099.130 (64 位)

python 复制代码
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By


if __name__ == '__main__':
    # 谷歌浏览器位置
    CHROME_PATH = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
    # 谷歌浏览器驱动地址
    CHROMEDRIVER_PATH = (r'C:\Program Files\Google\Chrome\chromedriver-win32_120.0.6099.109\chromedriver-win32'
                         r'\chromedriver.exe')
    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("useAutomationExtension", False)
    driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)
    options.binary_location = CHROME_PATH
    driver.get('https://www.baidu.com')
    print(driver.title)

    #
    wait = WebDriverWait(driver, 30, 0.5)

    # 获取属性的值 style
    element = wait.until(lambda diver: driver.find_element(By.ID, 'hotsearch_data'))
    style = element.get_attribute('style')
    print(type(style), style)

    # 获取输入框的值
    id = 'kw'
    element = wait.until(lambda diver: driver.find_element(By.ID, id))
    element.send_keys('你好')
    time.sleep(5)
    text = element.get_attribute('value')
    print(text)


'''
运行结果:
百度一下,你就知道
<class 'str'> display: none;
你好
'''
复制代码
'''
参考:
https://blog.csdn.net/weixin_45265081/article/details/121692135
'''
相关推荐
大模型真好玩19 分钟前
深入浅出LangChain AI Agent智能体开发教程(十)—LangChain搭建数据分析智能助手
人工智能·python·mcp
七夜zippoe1 小时前
Python性能优化实战(三):给内存“减负“的实用指南
python·内存·优化
WSSWWWSSW6 小时前
Seaborn数据可视化实战:Seaborn数据可视化基础-从内置数据集到外部数据集的应用
python·信息可视化·数据分析·matplotlib·seaborn
Small___ming6 小时前
Matplotlib 可视化大师系列(七):专属篇 - 绘制误差线、等高线与更多特殊图表
python·信息可视化·matplotlib
CodeCraft Studio8 小时前
3D文档控件Aspose.3D实用教程:使用 C# 构建 OBJ 到 U3D 转换器
开发语言·3d·c#·3d渲染·aspose·3d文件格式转换·3d sdk
superlls9 小时前
(Redis)主从哨兵模式与集群模式
java·开发语言·redis
荼蘼9 小时前
CUDA安装,pytorch库安装
人工智能·pytorch·python
杨荧10 小时前
基于Python的农作物病虫害防治网站 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python
chenglin01610 小时前
C#_gRPC
开发语言·c#
骑驴看星星a10 小时前
数学建模--Topsis(Python)
开发语言·python·学习·数学建模