Rayrun
← Back to Discord Forum

Having trouble interacting with page elements without using time.sleep() with playwright-pytest

I'm testing a Chrome extension at work and most of the page elements aren't click-able or visible without using time.sleep() first for about 3 seconds. I'm using locators with xpath. I have a context for my extension that's yielded from conftest.py, inhertied by each pytest function, and then then I generate a new page with page = context.new_page() and navigate to my extension's pop up page like page.goto(chrome://extension_id/ui/popup/popup.index). After that I'm trying to click on a button element on that page. Auto wait doesn't seem to be working and using page.wait_for_element() before clicking the element or expecting it to be visible using expect(element).to_be_visible() doesn't seem to work. My boss doesn't want me to use time.sleep() for obvious flakiness reasons. Have you had to overcome a situation like this using python? How did you get past it?

This thread is trying to answer question "How to interact with page elements without using time.sleep() in playwright-pytest?"

19 replies

Try buttonelement.dispatch_event('click')

On my other account now. Thanks for the suggestion but it didn't work for me.

Just simply wait for the locator with element.wait_for() https://playwright.dev/python/docs/release-notes#locatorwait_for

I tried that and playwright still won’t find the element for some reason without using time.sleep() first it’s weird.

Sounds like you maybe use python with async and forgot an await?

Unfortunately, no. I’m using sync.

Does click keep retrying to click and run into test timeout or will the click exit earlier for example because the locator resolves to multiple elements?

It seems like the former with it hitting the timeout which is longer than the 3 seconds that I’m using with time.sleep(). I double checked and the xpath I’m using only shows up once in the page. Is there some sort of logging in playwright where I can get a better idea of what’s happening?

Thanks. I’ll try that.

You could alternatively try the --tracing cli argument for pytest

Good call. I just found that.

try element.highlight() page.pause()

so, the execution will pause, you can review the highlighted element

I tried this in inspector last night and it finds and highlights the correct page element. Another thing I noticed is that running things in the inspector always pauses where my script tries to click the "hard-to-find" and highllghts the button without the element.hightlight() or the page.pause(). If I click play in the inspector after that pause it will always find the element (with or without the element.wait_for()) and click it without the time.sleep() but it seems like it's relying on the pause from teh inspector because when I run that same test with just pytest and no time.sleep() (with or without the element.wait_for()), it fails at that point.

Another thing I'm thinking may be causing issues is that when I create the page that has the "hard-to-find-button", it's the first page I'm creating and it always creates two pages. One just stays as a blank tab and the other gets the navigation from page.goto(). It seems like this could be causing problems or is at least worth elminating to narrow things down. I think I read somewhere that creating the extra blank tab with the first page created is a playwright bug.

When I create my first page I do so by using the context that was defined in conftest and inherited as an argument in my test like: page_one = context.new_page() main_page.set_page(page_one) page_one = context.new_page() is what always brings up two tabs with one that always remains blank. Seems like this could cause some problems. main_page.set_page() is where I pass the page object to my main page class which is where I have all my methods that interact with that page.

Maybe this isn't my issue though as it seems from the inspector, like it is finding my element, it just can't interact with it without a slight pause first.

I forgot to mention that I used Pick Locator option in the inspector to refine how I was creating my locator to no avail.

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 [email protected].