Rayrun
← Back to Discord Forum

How to run some actions synchronously in an async function?

maharishipachecoposted in #help-playwright
Open in Discord
maharishipacheco
maharishipacheco

Hi, I need to run two actions synchronously in an async function. How can I do this? If I await every step when playwright switches between browser_contexts, the previous step is lost, so when it comes back I cannot find what it was doing. How can I link 2 steps together in a single block?

These are currently my two lines of code

await page.get_by_role("textbox", name="To").fill(destination)
await page.get_by_text(destination, exact=True ).click()

What I would need is something like:

await page.get_by_role("textbox", name="To").fill(destination)
page.get_by_text(destination, exact=True ).click()

But I get an error if I try to run this code. In essence, once it fills the destination information in the cell, it has to immediately click on the text that appears. If it switches to another tab it will be lost.

This thread is trying to answer question "How can the user run two actions synchronously in an async function without losing the previous step when playwright switches between browser contexts?"

23 replies
tphillips8117

You should have a different page object for each tab

maharishipacheco
maharishipacheco

Hmm, I do. I create a completely dfferent browser context and page for each tab

tphillips8117

your first two lines of code should work fine if you are dealing with the same page object

tphillips8117

what error are you getting?

maharishipacheco
maharishipacheco

for some reason it is not working. when playwright switches between browswer contexts, the dropdown options disappear

tphillips8117

why is playwright switching between browser contexts?

maharishipacheco
maharishipacheco

I am getting a timeout error because it has nothing to click

maharishipacheco
maharishipacheco

so i can use dfferent proxies on eac

tphillips8117

you're switching browser contexts between the fill() and the click()?

maharishipacheco
maharishipacheco

no! I am running 3 async searches, each on its own browser context so each has a different proxy.

maharishipacheco
maharishipacheco

when my function gets to the two lines above. PW somtimes switches between tabs while the info is loading. but by the time it comes back the option in the dropdown menu has disappeared, hence i get a timeout error, since it can't find anything to click

tphillips8117

How are you running the 3 searches? in a Promise.all() or something?

tphillips8117

Or are you running 3 browsers at once?

tphillips8117

(3 "projects"?)

maharishipacheco
maharishipacheco

yes, 3 projects asynchronousl

tphillips8117

does it work in headless mode?

tphillips8117

(You're mentioning tabs, but you mean browser windows)?

maharishipacheco
maharishipacheco

yes, exactly. sorry

maharishipacheco
maharishipacheco

and no, it was not working on headless either, that is why I began running it in headful to pinpoint the issue

tphillips8117

does it work with just 1 browser?

maharishipacheco
maharishipacheco

yes, it does

maharishipacheco
maharishipacheco

so, is there a way to link both instructions in a synchronous block?

refactoreric

Can you share more of your code? You were mentioning you create multiple browser contexts but I don't see where you do that, and I don't see how you coordinate the actions on multiple browser contexts.

My hunch is that something is wrong there.

And do you use any threading?

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