Djent
Djent

Reputation: 3467

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?

Upvotes: 4

Views: 7362

Answers (2)

hardkoded
hardkoded

Reputation: 21607

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())

Upvotes: 4

hardkoded
hardkoded

Reputation: 21607

Another possible solution is waiting for the final selector.

page.locator(".desiredClassName").wait_for()

Upvotes: 3

Related Questions