Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to switch to the alert.
Selenium Java
Alert alert = driver.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after alert()
Using alert without switchTo()
✗ Incorrect
In Selenium Java, to switch to an alert, you use driver.switchTo().alert() with parentheses to call the method.
2fill in blank
mediumComplete the code to accept the unexpected alert.
Selenium Java
driver.switchTo().alert().[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dismiss() instead of accept()
Trying to close() the alert
✗ Incorrect
To accept an alert (click OK), use accept() method on the alert object.
3fill in blank
hardFix the error in the code to get alert text.
Selenium Java
String alertText = driver.switchTo().alert().[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python style get_text
Using non-existent methods like getAlertText
✗ Incorrect
The correct method to get alert text is getText() with camel case and parentheses.
4fill in blank
hardFill both blanks to handle alert and print its text.
Selenium Java
Alert alert = driver.[1](); System.out.println(alert.[2]());
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using accept() instead of getText() when printing
Not switching to alert before accessing it
✗ Incorrect
First, switch to the alert with switchTo().alert(), then get its text with getText().
5fill in blank
hardFill all three blanks to handle unexpected alert by getting text, accepting it, and printing the message.
Selenium Java
Alert alert = driver.[1](); String message = alert.[2](); alert.[3](); System.out.println(message);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling accept() before getText() causing lost message
Using dismiss() instead of accept()
✗ Incorrect
Switch to alert, get its text, accept the alert to close it, then print the message.