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
'''
相关推荐
百***480723 分钟前
Python使用PyMySQL操作MySQL完整指南
数据库·python·mysql
hateregiste30 分钟前
C语言中如何优雅、准确、高效地设计和处理输入输出
c语言·开发语言·scanf·输入输出
SundayBear32 分钟前
C语言复杂类型声明完全解析:从右左原则到工程实践
c语言·开发语言·数据结构·嵌入式
PNP Robotics41 分钟前
PNP机器人上海宝山智能机器人年会发表机器人10年主题演讲演讲
人工智能·python·机器人
90后小陈老师43 分钟前
用户管理系统 05 实现后端注册功能 | Java新手实战 | 最小架构 | 期末实训 | Java+SpringBoot+Vue3
java·开发语言·spring boot·后端·spring·maven·mybatis
___波子 Pro Max.1 小时前
Python获取当前脚本目录路径
python
努力成为大牛吧1 小时前
Pycharm 接入 Deepseek API完整版教程
ide·python·pycharm
闲人编程1 小时前
Python对象模型:一切都是对象的设计哲学
开发语言·python·模型·对象·codecapsule·下划线
列逍1 小时前
深入理解 C++ 智能指针:原理、使用与避坑指南
开发语言·c++
二川bro1 小时前
Python大语言模型调优:LLM微调完整实践指南
开发语言·python·语言模型