playwright-pytest
playwright-pytest copied to clipboard
Can't click or find elements on page without a slight pause/time.sleep() first
HI Playwright team!
I'm having an issue reliably interacting with page elements that is currently only being solved by using a time.sleep(3) before trying to click or expect an element to be on the page.
I'm using VScode on a MacBook with Ventura 13.5.1 and using sync playwright-pytest with python 3.10.
I'm creating a page from a context (for the extension I'm testing) that was generated in conftest.py and passed as an argument into my test.
Generating a context in conftest.py
import pytest
from typing import Generator
from pathlib import Path
from playwright.sync_api import Playwright, BrowserContext
@pytest.fixture(scope="session")
def context(playwright: Playwright) -> Generator[BrowserContext, None, None]:
path_to_extension = Path(__file__).parent.joinpath("path to extension")
context = playwright.chromium.launch_persistent_context(
"",
headless=False,
args=[
f"--disable-extensions-except={path_to_extension}",
f"--load-extension={path_to_extension}"
]
)
yield context
context.close()`
**In my test I'm creating the page like this**
`page_one = context.new_page()`
**Then I'm navigating to my extension page's pop up like this**
`page_one.goto(f'chrome-extension://{EXTENSION_ID}/ui/popup/popup.html')`
**Then I'm creating my locator like this (locator was suggested by the Playwright GUI inspector)**
`page_one.page.get_by_role(role, name=name)`
**Then I'm trying to click on the returned Locator or expect to be on the page like:**
`locator_element.click()
or
expect(locator_element).to_be_visible()
These only seem to work with a pause from time.sleep(3) in front of them or the pause from the GUI inspector after navigating to the page and before trying to click or expect the element like this
time.sleep(3)
locator_element.click()
or
time.sleep(3)
expect(locator_element).to_be_visible()
I have tried:
- Using
element_locator.wait_for()before trying to interact with the element. - Using timeout=some amount of time up to 30000 ms
- Passing every available parameter to .click() and expect().to_be_visible()
- Using async/await which I got to work with pytest using an extension I found on Pypi but I couldn't get it to work with my extension context for some reason.
- And probably a couple other things that I'm not remembering when I was throwing the kitchen sink at this.
Any suggestions for how to accomplish this in a more dynamic way than using time.sleep()? Using a static wait like this isn't going over well at work. :/
In my case I've created a simple function:
def wrapped_click(locator):
locator.wait_for()
locator.click()
without it Playwright seems to be... too fast?
I guess it depends also on how the frontend is built (I have an app which is over 10 years old, so there are parts which doesn't meet current standards of UI dev).
@aspenboy Was this on the popup page of a chrome extension? I have the same thing you're suggesting for the other pages of my extension and it works fine. It's only on the popup.html page of my extension that i have to use a time.sleep(3) before trying to interact with a locator.
@aspenboy I just realized that wasn't included in the original post. I narrowed it down to the popup.html page after I made this post.
Sounds not like a Playwright issue and more like an issue of the extension you are testing. Playwright is like a very fast user, and often this is too fast for the websites users are testing, ending up in race conditions, hydration issues etc.
Closing as per above. Feel free to re-file for further questions.