popup ads when button was clicked

Good day.

I’m having trouble in buttons/links that embedded with popup ads its either open a new window (for firefox) or new tab (for chrome). What’s more is that Katalon cannot locate the buttons after returning to the main window (after i manually close it) since it was first link to ads and we have to click it again to proceed.

I also want if there is a workaround to close any popup window/tab Ads if it appears. This one have no accept or decline button just pure Ads.

I may be wrong but I think we may handle such situations by using try-catch. First get the primary/main window handle by “String currentWindow = driver.getWindowHandle();”, Put your codes which will get executed after 1st click in try block and in catch block put “driver.switchTo().window(currentWindow);”.