About this lesson
Selenium Python custom modal popup: wait for it to open and read it as plain HTML
Selenium Python AssignmentsIntermediateAlerts and Popups
How it works
Not every popup is a browser alert. A custom modal is built from HTML and CSS, so it is part of the page. If you can inspect it in the developer tools, it is an element, and driver.switch_to.alert raises NoAlertPresentException for it. Find it with find_element like anything else.
The modal exists in the document before you open it. It is a div whose class list starts as modal fade and gains show when open. While closed, is_displayed() is False and the body text is an empty string, though the words are in the page. The class also changes a moment after the click because of an animation, so reading it straight away shows the old value.
So wait on the body, not the class. The show class arrives while the fade is still at zero opacity, so a wait on the class alone can return early and read a blank. visibility_of_element_located with a CSS selector that starts at the modal returns the body part once it can really be seen. The modal also has a title, so read the body, not the heading. Finally click Close inside the modal, found by its visible text.
About the assignment
Open the custom modal, wait until it is really open, read the sentence in its body, and close it.
What you will do
- Click the Open Modal button, found by its visible text.
- Create a
WebDriverWaitand wait until the body part inside the modal is visible, keeping the element it returns. - Print that element's
textafter the label. - Click the Close button that sits inside the modal.
Common mistakes
Reaching for switch_to.alert
The modal is not an alert. The call raises
NoAlertPresentException: no such alert, because no browser dialog is open.Reading the body too soon
Right after the click the modal is still hidden, so
textis an empty string. The label prints and nothing follows. Wait for the body to be visible first.Clicking Close too early
While the modal is still hidden, its Close button cannot be clicked. The call raises
ElementNotInteractableException. Wait for the body to be visible first.
In a real test suite
In a real suite, a modal is a page object of its own, with methods such as open(), read_body() and close(). Waiting for the open state inside open() means tests never meet a half-animated dialog.
The next lesson moves to another thing that hides content from the driver: a frame.
Related lessons
- Accept a Simple AlertThe browser alert, which this page is the opposite of.
- Wait Until an Element Is VisibleIntroduces waiting for an element to become visible.
- Enter a Frame by ID with switch_to.frameAnother case where content is on screen but invisible to a plain lookup.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Interactions/Alerts")
# Open the modal, wait until its body is visible, print the body, and close it
finally:
driver.quit()