跳到主要内容

获取属性

根据属性名称从DOM元素获取属性值。

用法
$(selector).getAttribute(attributeName)
参数
名称类型详情
attributeNamestring请求的属性
示例
index.html
<form action="/submit" method="post" class="loginForm">
<input type="text" name="name" placeholder="username"></input>
<input type="text" name="password" placeholder="password"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
getAttribute.js
it('should demonstrate the getAttribute command', async () => {
const form = await $('form')
const attr = await form.getAttribute('method')
console.log(attr) // outputs: "post"
})
返回值
  • <String|null> return: 属性的值,如果元素上未设置则返回null。

Welcome! How can I help?

WebdriverIO AI Copilot