I get the "element is outside the viewport" error and still retrying action until the test time runs out when trying to click the button in the chromium browser. but the error only occurs in chromium, if I run it in firefox or in webkit then the test runs fine. I didn't set a value for the viewport, it should work fine because I used the default from playwright.
Test timeout of 60000ms exceeded.
Error: locator.click: Target closed
=========================== logs ===========================
waiting for locator('#button-show-modal-create-activity')
locator resolved to <button type="button" class="btn-v2 btn btn-jejakin-whit…>Add Activity</button>
attempting click action
waiting for element to be visible, enabled and stable
element is visible, enabled and stable
scrolling into view if needed
done scrolling
element is outside of the viewport
retrying click action, attempt #1
waiting for element to be visible, enabled and stable
element is visible, enabled and stable
scrolling into view if needed
done scrolling
element is outside of the viewport
I have also done various ways to click the button but nothing works.
// await page.locator("#button-show-modal-create-activity").click();
// await page.locator("_react=button[id='button-show-modal-create-activity']").click();
// await page.getByRole('button', { name: 'Add Activity' }).click();
// const button = page.locator("//button[text()='Add Activity']");
// await button.scrollIntoViewIfNeeded();
// await button.click();
// const button = page.locator("_react=button[id='button-show-modal-create-activity']");
// await button.waitFor({ state: 'visible' });
// await button.click();
and the element is not located inside an iframe. fyi: my application is build on react js, and my device viewport is 1660 × 946
This thread is trying to answer question "Why does the 'element is outside the viewport' error occur in Chromium when trying to click a button using Playwright and how can it be resolved?"
https://discord.com/channels/807756831384403968/1128389444563632271
If that is the case I had the same problem, only on mobile tho
I have "fixed" it by forcing pw to click on an element that is near my desired element. In my case i had to scroll to the top of the page in order for my element to appear (none of the scrolls, mouse, scroll into view methods have worked) and to do so i forced pw to click on a random element on the top of the page so that the page would scroll up and reveal my desired element.
Related Ask AI answer for "What is the reason for the 'element is outside of the viewport' error in Playwright and how can it be fixed?".
This error occurs when the element you're trying to interact with isn't visible on the screen. It could be below the visible area of the webpage or hidden behind other elements.
One way to fix this is to scroll the page until the element is visible. You can use Playwright's scrolling methods for this:
await page.scrollIntoView();
await element.scrollIntoView();
Alternatively, you can force Playwright to click the element even if it's not visible. Be careful with this approach as it might lead to unexpected behavior:
await element.click({ force: true });
You can also wait until the element is visible or meets certain criteria:
await page.waitForSelector();
await page.waitForFunction();
In some cases, changing browser settings or emulation options can help. For example, improving page loading speed by adjusting network emulation settings in Chrome DevTools Network panel.
Remember, the key to resolving this error is to ensure the element is visible and ready for interaction. Check out Mastering Playwright Test Automation: Your Comprehensive Cheat Sheet for more tips.
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 [email protected].