Skip to main content

isFocused

Overview

Use the isFocused command to determine whether a given DOM element is focused.

The command returns true if the selected element is focused; otherwise, it returns false.

If multiple elements match the selector, the command returns true if at least one of the elements is focused.

Usage

await browser.$(selector).isFocused();

Usage Examples

index.html

<input name="login" autofocus="" />

isFocused.js

it("should detect the focus of an element", async ({ browser }) => {
await browser.url("/");
const loginInput = await browser.$('[name="login"]');
console.log(await loginInput.isFocused()); // outputs: false

await loginInput.click();
console.log(await loginInput.isFocused()); // outputs: true
});

References

We'd like to give credit to the original WebdriverIO docs article, from which we drew some information while writing our version.