← Back to Discord Forum

Select first visible element

HI all, I'm trying to automate dete selection from a date picker on a form (picture). If I try to set it to pick always today's date, like this:

var startDate = DateTime.Now;
        await Page.GetByPlaceholder("Start date").ClickAsync();
        await Page.Locator($"//span[text()='{startDate.Day}']").ClickAsync();

then it will resolve to two elements, as there are two 26th on the calendar right now. Is there a way to set to always pick the first visible/clickable 26?


This thread is trying to answer question "Is there a way to set to always pick the first visible/clickable date?"

0 replies


Sure, you can set Playwright to always select the first visible clickable date. Playwright's actionability checks ensure elements are ready for interaction before performing actions. These checks include verifying if an element is attached to the DOM, visible, stable, and can receive events.

Here's how you can use the click method to select the first visible clickable date:

await page.click('your-selector-for-the-date-element');

Just replace 'your-selector-for-the-date-element' with a valid selector that targets your date element. Playwright will automatically wait for the element to be ready for interaction and perform a human-like click on it.

If there are multiple elements matching your selector and you want to ensure only one element is selected, you can use page.waitForSelector or specify more specific selectors. This way, you'll always pick the first visible clickable date.

For more tips on writing efficient Playwright test scripts, check out this blog post.

Related Discord Threads

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.