Skip to content
CodeKruPro

Combine a Wait, a Frame, and an Alert in One Script

80/80

Combine a Wait, a Frame, and an Alert in One Script

One script can visit three pages. Wait until the 5-second message is visible, read the nested child frame and return to the top document, then accept the simple alert. Print the three texts you observed. Use one function or a small page object. Assert at least one text. Quit with finally or a context manager.

Task:

  1. Wait for #appearing5sec and keep its text
  2. Read the child frame text, then return to the top document
  3. Accept the simple alert and keep its text
  4. Print the three lines and assert one of them

Aim for a reliable wait, a locator you chose yourself, and cleanup that runs even when a line fails.

Print the lines below exactly.

Expected output:

Appeared: Message appeared after 5 seconds
Frame: Child frame
Alert: This is a simple alert!

About this lesson

Selenium Python wait, frame and alert in one script with safe cleanup

Selenium Python AssignmentsAdvancedStructure

How it works

This lesson is a rehearsal for a real test. You use three separate skills in order, and each leaves the driver in a certain state that the next step must respect. A wait handles time, a frame handles where the driver is looking, and an alert handles a dialog that blocks the page.

First, the wait. The message on the Appearing Elements page is in the HTML from the start but hidden, so a plain find returns it with empty text. WebDriverWait with until polls a condition. The condition takes a tuple, so visibility_of_element_located needs double brackets. Passing two separate arguments raises TypeError. A timeout raises TimeoutException.

Second, the frame. The frames page has a frame inside a frame. You enter the outer one with switch_to.frame, enter the inner one the same way, read the text, and call switch_to.default_content to return to the top. Third, the alert. After the click, switch_to.alert gives an Alert object. Read its text before you call accept, because after the accept a second read raises NoAlertPresentException. While the alert is open, even driver.title raises UnexpectedAlertPresentException.

About the assignment

Visit three practice pages in one script, keep one piece of text from each, and print them. Then assert one of them, so the script fails loudly if the page changes. Cleanup must run even when a line fails.

What you will do

  1. Wait for the delayed message to become visible, then store its text in a variable.
  2. Open the Frames page, enter the outer and inner frames, store the heading text, and return to the top document.
  3. Open the Alerts page, trigger the simple alert, store its text, then accept it.
  4. Read the texts through one small function or page object, print the three labelled lines, add an assert on a value you stored, and wrap all of it in try and finally or a with block.

Common mistakes

  • Reading the message without waiting

    The element exists straight away, so no exception warns you, but is_displayed is False and the text is empty. Use the wait.

  • Staying inside a frame

    A search for a page element from inside a frame raises NoSuchElementException. Return to the top document before anything outside the frame.

  • Reading the alert after accepting

    The text is gone once the dialog closes. Store it first, then accept.

In a real test suite

Real suites split this into small functions or page objects, one per area, with a wait helper shared by all. Each step returns the text it read, and the test asserts on them.

That closes the track, which moved from tables and cookies to helpers, classes and cleanup.

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

# On https://testkru.com/Interactions/AppearingElements wait until the 5-second message is visible and remember its text.
# On https://testkru.com/Interactions/Frames read the nested child frame, then return to the top document.
# On https://testkru.com/Interactions/Alerts accept the simple alert and remember its text.
# Print the three texts, assert one of them, and quit with finally or with.