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
'''
相关推荐
霸道流氓气质4 小时前
SpringBoot中通用工具类库(Utils)封装与使用实践
spring boot·后端·python
孫治AllenSun5 小时前
【DataX】生产环境搭建DataX集群案例
java·开发语言·jvm
Draina6 小时前
CBC填充预言攻击-CBC Padding Oracle Crypto Attack
python·安全·web安全·网络安全·密码学·安全性测试
c238566 小时前
把 C++ 内存分配拆透:new 与 malloc 的三层血缘
开发语言·c++·算法
Angel Q.6 小时前
特征提取 | DINO 到 DINOv3
图像处理·python
不如语冰7 小时前
AI大模型入门-pytorch-张量2 tensor创建
python
moonsims7 小时前
星闪在跨域无人化系统作用
开发语言·php
NPE~7 小时前
[AI]Agent开发——ADK框架使用
人工智能·python·ai·教程·adk·agent开发
矮个史蒂芬7 小时前
统计verilog .v文件中output 的bit数
python
gwf2167 小时前
磨损均衡算法(Wear Leveling)——SSD如何让每块闪存“公平退休“?
运维·数据库·人工智能·python·嵌入式硬件·算法·智能硬件