Search code examples
playwrightwebautomationplaywright-python

Wait for class to change in playwright


I need to wait for API request to finish, which is indicated by loading class in one of the elements.
I can get the current class value with the following code:

page.locator("selector").first.evaluate("node => node.className")

I know I can write code that would call that every 0.1s to in order to wait for the change, but I was thinking that playwright has some better mechanisms to do that.
Is it possible to wait for class to have a certain value? Something like that:

page.locator("selector").first.wait_for_value("node => node.className", "desired_value")

Can it be done or do I need to write my own such functions?


Solution

  • You can use the wait_for_function. e.g.

    locator = page.locator("selector").first;
    page.wait_for_function("node => node.className === ""desired_value""", arg=locator.element_handle())