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
'''
相关推荐
Hello_Pyhx1 小时前
Deep-Live-Cam 源码配置思路:环境、模型与执行后端分开排查
python
不积跬步无以至千里mmbb262 小时前
rust_analysis
开发语言·后端·rust
张张123y2 小时前
后端第十六课:Multi-Agent 多智能体(让多个 Agent 分工协作)
大数据·数据库·人工智能·python·deepseek·dsh·agent 框架
全栈练习生2 小时前
大规模智能体仿真
python·ai
暴力求解2 小时前
C++ ---继承(二)
开发语言·c++·算法
code2cat2 小时前
【随笔】vLLM前缀缓存:相同的开头怎样少算一次
人工智能·python·缓存·vllm
陈飞EXCEL3 小时前
北京杰飞 IT 旗下妙码生花少儿Python编程上线两大学习系统
开发语言·python·学习
代龙涛3 小时前
WordPress Query 查询机制详解(WP_Query)
开发语言·后端·php·wordpress
Anesthesia丶3 小时前
基于 Qwen3-TTS 的多语言智能配音 Web 应用(Qwen3 TTS Studio)
python·ai·qwen-tts·agci
znsoft3 小时前
MaClaw(码卡龙)技术拆解:一个 Go + React 的可自进化智能体平台
开发语言·react.js·golang