Skip to content

[Question] How to get direct descendant from a locator #9039

@A-ZC-Lau

Description

@A-ZC-Lau

How do I target only direct children from a locator that already has a target?

Example:

import { test, expect, Page } from "@playwright/test";

test("direct descendant", async ({ page }) => {
    await page.goto("http://www.uitestingplayground.com/sampleapp");

    const containerLocator = page.locator("div.container");
    const immediateChildren = containerLocator.locator("> div");

    const count = await immediateChildren.count();
    expect(count).toBe(4);
});

I get the error locator.count: Unexpected token ">" while parsing selector "> div"

Even css="> div" doesn't work

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions