Rayrun
← Back to Discord Forum

Foolproof way to check if element is visible

Basically, I want to make sure that the element is really visible on the screen AND stable. That specific element by default is hidden because its parent element has a 0px height. When "opening" the collapsed parent element, the parent element's height is increased in an animation until the sub element which I want to check for is visible.

On here: https://playwright.dev/docs/actionability#assertions you can find all the different assertions you can do on an element.

Is there an all-in-one way/function I can use to check if an element is attached, visible and stable at the same time? And, for this specific scenario, would it ensure that the element (which is part of the collapsing parent element) is visible?

This thread is trying to answer question "Is there an all-in-one way/function I can use to check if an element is attached, visible and stable at the same time? And, for this specific scenario, would it ensure that the element (which is part of the collapsing parent element) is visible?"

0 replies

Answer

Sure, you can use Playwright's waitForSelector method to check if an element is attached, visible, and stable. Here's how you do it:

First, import Playwright:

const { chromium } = require('@playwright/test');

Launch a new browser instance:

const browser = await chromium.launch();
const page = await browser.newPage();

Navigate to your desired URL:

await page.goto('https://ray.run');

Now, use the waitForSelector method:

const selector = 'your-element-selector';
try {
  await page.waitForSelector(selector, { state: 'visible', strict: true });
  console.log('The element is attached, visible, and stable.');
} catch (error) {
  console.log('The element did not satisfy all conditions within the timeout period.');
}

The state option set to "visible" makes Playwright wait for the element to have a non-empty bounding box and no visibility:hidden style. The strict option ensures that only elements that fully satisfy these conditions are considered.

This approach should work even when elements are part of collapsing parent elements. For more details and examples, refer to the official Playwright documentation.

Related Discord Threads

TwitterGitHubLinkedIn
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.