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
'''
相关推荐
Tizzy JJ5 小时前
Python + pytest 接口自动化测试框架实战:从零搭建企业级项目骨架
开发语言·python·pytest
海兰5 小时前
【 Python 量化交易】第8章:量化工具箱
开发语言·python
牛油果子哥q5 小时前
C++大型项目工程精讲:CMake完整实战、静态库&动态库、模块化拆分、单元测试、gdb调试、性能工具、工程踩坑全解
开发语言·c++·单元测试
Dream Cosmos5 小时前
C++ 多态上篇:从 virtual 到抽象类,彻底理解多态的使用
开发语言·c++
TheBestRucy5 小时前
Python九阳神功之柒:数据分析三剑客·执剑问道
开发语言·python·数据分析
TheBestRucy5 小时前
Python九阳神功之陆:数据库持久化与缓存·乾坤大挪移
数据库·python·缓存
qq_339191145 小时前
go cpu占比高排查,cpu100%排查,go pprof cpu命令
开发语言·后端·golang
西西弗Sisyphus5 小时前
Qt 配置文件图标和文件版本信息
开发语言·qt
坐吃山猪5 小时前
IDEA调试中Evaluate常用操作
java·python·intellij-idea