I want to fill a two-steps login form with Playwright.
The Playwright execution is controlled by a library, so I cannot change the execution context.
To fill my form, I can only use some Javascript code that will be executed by evaluate
. I have no access to the page
object.
In the following example, the first form is correctly loaded, but filling the password field fails. I suppose this is because the code is executed before the page is completely loaded.
How can I make this Javascript code wait for the second page to be completely loaded?
Again, I cannot edit the Playwright context, but if there is a generic solution, I could open a PR upstream.
Here is a schematic snippet of my issue (this is in Python, but that should not be relevant to the actual issue):
from playwright.sync_api import sync_playwright
# this is the only things I can edit:
javascript = """
document.querySelector('input[name=login]').value = "admin"
document.querySelector('form').submit()
# <-- How to wait for the page to be loaded here?
document.querySelector('input[name=password]').value = "password"
document.querySelector('form').submit()
"""
# this is the library code I cannot edit
with sync_playwright() as pw:
browser = pw.firefox.launch()
context = browser.new_context()
page = context.new_page()
page.goto('http://localhost:5000')
page.evaluate(javascript)
browser.close()
The playwright error:
playwright._impl._errors.Error: Page.evaluate: document.querySelector(...) is null
@debugger eval code line 234 > eval:4:18
evaluate@debugger eval code:234:30
@debugger eval code:1:44