I am trying to automate a login process where a promotional pop-up appears before the main dashboard. My Selenium script fails to find the button inside the pop-up, throwing a NoSuchElementException or an ElementClickInterceptedException. I’ve tried using standard find_element calls, but they seem to target the background page instead. Should I be using WebDriverWait for the modal to be visible, or do I need to switch the driver’s focus to an iframe or a new window handle to interact with the button?
3 answers
Interacting with pop-ups usually requires WebDriverWait combined with ExpectedConditions. Most modern modals are just div elements that appear dynamically; therefore, you don't usually need to switch windows. Instead, you must wait for the element to be clickable to avoid timing issues. Use EC.element_to_be_clickable to ensure the modal has finished its animation before attempting the click. If the pop-up is actually an advertisement or a separate frame, you must use driver.switch_to.frame() first. Always check the HTML source—if the button is inside an <iframe> tag, your script will never find it unless you explicitly switch the context to that specific frame.
Have you checked if the pop-up is a browser-level alert (like a Javascript 'alert' or 'confirm' box) or a custom HTML modal built with a framework like Bootstrap or React?
If the button is stubborn, I often use a JavaScript click as a workaround. driver.execute_script("arguments[0].click();", element) bypasses the visual check and clicks the hidden node.
I agree with Melissa that JS clicks are a great "nuclear option." While it's better to fix the timing with waits, sometimes overlapping invisible layers make standard Selenium clicks impossible, and a quick script execution saves hours of debugging.
Robert, that's an important distinction. In my case, it's a Bootstrap modal. I was mistakenly trying to use driver.switch_to.alert, which only works for those old-school grey browser boxes. For Bootstrap, I followed Deborah's advice on using WebDriverWait. I also found that sometimes you have to wait for the "modal-backdrop" to disappear or for the modal's "aria-hidden" attribute to change to false before Selenium can reliably send the click command without getting an interception error.