How to stop a page refreshing with a timeout - C# and Playwright.Net

I have a script below that refreshes the page automatically if an element is not visible using automation script, and this works but I want it to stop refreshing after a period of time e.g 20 seconds or a number of times if the element is not visible so the test will fail and so it doesn’t end up infinitely refreshing. The code below works if the element is visible but if the element is not visible, it keeps refreshing infinitely. Any idea how to resolve it please ? Thanks

 do {
await Page.ReloadAsync(new PageReloadOptions() { Timeout = 5000 }); } 
while (!await Page.IsVisibleAsync("input[name='elementname']"))

Just add a counter

int retries = 0;
bool isElementVisible = false;
do {
  await Page.ReloadAsync(new PageReloadOptions() { Timeout = 5000 });
  isElementVisible = await Page.IsVisibleAsync("input[name='elementname']");
  retries += 1;
while (!isElementVisible && retries < 5)
1 Like

Thank you. I may need to add an if statement after the while since it just executes the next block of code but i want it to exit if it fails to load the element ?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.