Hint: Use switch_to.window(handle) to change browser tabs/windows [OK]
Common Mistakes:
Using switch_to.alert for windows
Passing window handle to switch_to.frame
Omitting the window handle argument
3. Given the following code snippet, what will be printed?
driver.get('https://example.com')
main_window = driver.current_window_handle
# Assume clicking button opens a new window
button = driver.find_element('id', 'openWindow')
button.click()
all_windows = driver.window_handles
for handle in all_windows:
if handle != main_window:
driver.switch_to.window(handle)
print(driver.title)
driver.close()
break
# Switch back to main window
driver.switch_to.window(main_window)
print(driver.title)
medium
A. Title of new window followed by title of main window
B. Only title of main window printed twice
C. Only title of new window printed
D. No output due to error
Solution
Step 1: Understand window switching and closing
The code clicks a button that opens a new window, then switches to it, prints its title, closes it, and breaks the loop.
Step 2: Switch back and print main window title
After closing the new window, the code switches back to the main window and prints its title.
Final Answer:
Title of new window followed by title of main window -> Option A
Quick Check:
Print new window title, then main window title [OK]
Hint: Switch to new window, print title, close, then switch back [OK]
Common Mistakes:
Not switching back to main window before printing
Trying to print after closing main window
Assuming both titles print simultaneously
4. Identify the error in the following Selenium Python code for handling an alert pop-up:
alert = driver.switch_to.alert()
alert.accept()
medium
A. Incorrect use of parentheses after switch_to.alert
B. Missing import for Alert class
C. alert.accept() should be alert.dismiss()
D. No error, code is correct
Solution
Step 1: Check method usage for alert switching
driver.switch_to.alert is a property, not a method, so it should not have parentheses.
Step 2: Correct the syntax
Remove parentheses: use alert = driver.switch_to.alert to get the alert object.
Final Answer:
Incorrect use of parentheses after switch_to.alert -> Option A
Quick Check:
switch_to.alert is property, no () [OK]
Hint: Don't add () after switch_to.alert, it's a property [OK]
Common Mistakes:
Adding parentheses after switch_to.alert
Confusing accept() with dismiss()
Not switching to alert before accepting
5. You have a test that opens multiple pop-up windows in sequence. Which approach correctly handles switching to each new window, closing it, and returning to the main window without errors?
hard
A. Store main window handle, loop through all handles, switch to each new window, close it, then switch back to main window after loop
B. Switch to each window and close it inside the loop, then switch back to main window after closing all windows
C. Switch to each new window, close it immediately, and switch back to main window inside the loop after each close
D. Close main window first, then switch to new windows one by one
Solution
Step 1: Save main window handle before opening pop-ups
Store the main window handle to return to it later safely.
Step 2: Loop through all window handles, switch, close, and return
For each new window, switch to it, close it, then switch back to main window inside the loop to avoid stale window errors.
Step 3: Avoid closing main window before others
Closing the main window first causes errors; it should be closed last or not at all during pop-up handling.
Final Answer:
Switch to each new window, close it immediately, and switch back to main window inside the loop after each close -> Option C
Quick Check:
Switch-close-switch back per window to avoid errors [OK]
Hint: Switch-close-switch back inside loop for each pop-up [OK]
Common Mistakes:
Closing main window before others
Switching back only after closing all pop-ups
Not storing main window handle before loop
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.