跳到主要内容

getCSSProperty

从给定选择器选中的DOM元素获取CSS属性。返回值格式便于测试。颜色通过rgb2hex解析,所有其他属性通过css-value解析。

信息

请注意,简写CSS属性(例如backgroundfontbordermarginpaddinglist-styleoutlinepausecue)将被展开以获取所有长格式属性,这会导致多次WebDriver调用。如果你对特定的长格式属性感兴趣,建议直接查询该属性。

Usage
$(selector).getCSSProperty(cssProperty, pseudoElement)
Parameters
NameTypeDetails
cssPropertystringcss property name
pseudoElementPseudoElementcss pseudo element
Examples
example.html
<label id="myLabel" for="input" style="color: #0088cc; font-family: helvetica, arial, freesans, clean, sans-serif, width: 100px">Some Label</label>
getCSSProperty.js
it('should demonstrate the getCSSProperty command', async () => {
const elem = await $('#myLabel')
const color = await elem.getCSSProperty('color')
console.log(color)
// outputs the following:
// {
// property: 'color',
// value: 'rgba(0, 136, 204, 1)',
// parsed: {
// hex: '#0088cc',
// alpha: 1,
// type: 'color',
// rgba: 'rgba(0, 136, 204, 1)'
// }
// }

const font = await elem.getCSSProperty('font-family')
console.log(font)
// outputs the following:
// {
// property: 'font-family',
// value: 'helvetica',
// parsed: {
// value: [ 'helvetica', 'arial', 'freesans', 'clean', 'sans-serif' ],
// type: 'font',
// string: 'helvetica, arial, freesans, clean, sans-serif'
// }
// }

var width = await elem.getCSSProperty('width', '::before')
console.log(width)
// outputs the following:
// {
// property: 'width',
// value: '100px',
// parsed: {
// type: 'number',
// string: '100px',
// unit: 'px',
// value: 100
// }
// }
})
Returns
  • <CSSProperty> return: The specified css of the element

Welcome! How can I help?

WebdriverIO AI Copilot