Skip to content
CodeKruPro

Read a Modal Without Treating It as an Alert

48/80

Read a Modal Without Treating It as an Alert

The custom modal is HTML in the page. It is not a browser alert. Do not use switch_to.alert. The buttons have no ids.

Task:

  1. Click Open Modal
  2. Wait until #customModal .modal-body is visible
  3. Print its text
  4. Click Close inside the modal

Print the lines below exactly.

Expected output:

Modal: This is a custom modal popup. You can test handling it in automation.

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

  1. Click the Open Modal button, found by its visible text.
  2. Create a WebDriverWait and wait until the body part inside the modal is visible, keeping the element it returns.
  3. Print that element's text after the label.
  4. 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 text is 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.

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.

main.py
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()