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
- Wait for the delayed message to become visible, then store its text in a variable.
- Open the Frames page, enter the outer and inner frames, store the heading text, and return to the top document.
- Open the Alerts page, trigger the simple alert, store its text, then accept it.
- Read the texts through one small function or page object, print the three labelled lines, add an
asserton a value you stored, and wrap all of it intryandfinallyor awithblock.
Common mistakes
Reading the message without waiting
The element exists straight away, so no exception warns you, but
is_displayedisFalseand 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.
Related lessons
- Wait Until an Element Is VisibleWaits until an element is visible, the first step here.
- Read a Nested FrameReads a nested frame, the second step here.
- Accept a Simple AlertAccepts a simple alert, the last step here.
- Return to the Top DocumentReturns to the top document after frame work.
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
# 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.