跳至主要內容

getCSSProperty

從給定選取器選取的 DOM 元素取得 CSS 屬性。傳回值會格式化為可測試的格式。顏色會透過 rgb2hex 解析,而所有其他屬性會透過 css-value 解析。

資訊

請注意,簡寫 CSS 屬性(例如 backgroundfontbordermarginpaddinglist-styleoutlinepausecue)將會展開以取得所有長寫屬性,導致多個 WebDriver 呼叫。如果您對特定的長寫屬性感興趣,建議改為查詢該屬性。

用法
$(selector).getCSSProperty(cssProperty, pseudoElement)
參數
名稱類型詳細資訊
cssProperty字串CSS 屬性名稱
pseudoElementPseudoElementCSS 虛擬元素
範例
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
// }
// }
})

歡迎!我能為您做什麼?

WebdriverIO AI Copilot