Skip to main content

previousElement

Overview

Use the previousElement command to get the previous sibling element of the selected DOM element.

Usage

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

Usage Examples

index.html

<div class="parent">
<p>Sibling One</p>
<p>Sibling Two</p>
<p>Sibling Three</p>
</div>

previousElement.js

it("should get text from previous sibling element", async ({ browser }) => {
const elem = await browser.$$("p");
const previousElement = await elem[1].previousElement();

console.log(await previousElement.getText()); // outputs: "Sibling One"
});

References

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