Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Webscraping
  • Beautiful Soup
  • Request
  • Scrapy
  • open cv
  • Data analysis
  • Machine learning
  • NLP
  • Deep learning
  • Data Science
  • Interview question
  • ML math
  • ML Projects
  • ML interview
  • DL interview
Open In App
Next Article:
How to handle Frames/iFrames in Selenium with Python
Next article icon

How to handle alert prompts in Selenium Python ?

Last Updated : 20 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Selenium’s Python Module is built to perform automated testing with Python. Alerts are a way to show popups in the browser for either accepting data or displaying data. Selenium provides methods to handle alerts of all kinds.

Selenium’s Python Module is built to perform automated testing with Python. Alerts are a way to show popups in the browser for either accepting data or displaying data. Selenium provides methods to handle alerts of all kinds. class selenium.webdriver.common.alert.Alert(driver) handles all alerts in Selenium Python. It contains methods for dismissing, accepting, inputting, and getting text from alert prompts. The two major tasks in alerts are accepting an alert or dismissing a alert.

Selenium provides two methods for the same:

Alert(driver).accept()
Alert(driver).dismiss()

Alert Methods

The major methods during handling of alerts in Selenium include –

  • accept() – Accepts the alert available.
  • dismiss() – Dismisses the alert available.
  • send_keys(keysToSend) – Send Keys to the Alert.
  • text – Gets the text of the Alert.

How to operate on an alert prompt using Selenium Python ?

To illustrate alerts, let’s write manual javascript alert and check various methods on the same. We have created an example link – https://ide.geeksforgeeks.org/tryit.php/WXYeMD9tD4

Python
from selenium import webdriver from selenium.webdriver.common.alert import Alert import time  # Create WebDriver object driver = webdriver.Firefox(executable_path="path_to_geckodriver")   # Make sure to specify the correct path to geckodriver  # Navigate to the webpage (Ensure the URL is correct without spaces) driver.get("https://ide.geeksforgeeks.org/tryit.php/WXYeMD9tD4")  # Wait for the alert to appear (you might need to adjust the time based on your page load time) time.sleep(3)  # Switch to the alert alert = Alert(driver)  # Get the alert text print(alert.text)  # Accept the alert alert.accept()  # Close the browser driver.quit() 

Output –

alerts-in-python-selenim

Terminal Output –

terminal-output-alerts-in-selenium-python


Next Article
How to handle Frames/iFrames in Selenium with Python

N

NaveenArora
Improve
Article Tags :
  • Python
  • Selenium
  • Software Testing
  • Python-selenium
Practice Tags :
  • python

Similar Reads

  • How to handle Frames/iFrames in Selenium with Python
    Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we can be running with Python. HTML outlines are utilized to isolate yo
    2 min read
  • How to Handle Alert in Selenium using Java?
    Imagine filling out a form online and accidentally missing some information. You only know if you made a mistake if the website tells you somehow, like with a pop-up message. This article explains what those pop-up messages are called in Selenium (alerts) and how to deal with them in your automated
    6 min read
  • How to use close() and quit() method in Selenium Python ?
    While doing stuff with selenium multiple browsers with multiple tabs will normally opens in order to close these tabs close() and quit() methods are used. close() method is used to close the current browser window on which the focus is set, on the other hand quit() method essentially calls the drive
    2 min read
  • How to get text of a tag in selenium - Python?
    Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. In this article, we will w
    1 min read
  • How to make Firefox headless programmatically in Selenium with Python?
    Selenium is a powerful tool for web automation and testing. You can use Firefox in headless mode when you need to automate web interactions without a visible browser window. Running Firefox headless allows you to perform tasks like automated testing, web scraping, and server-side rendering more effi
    6 min read
  • Python - find_element() method in Selenium
    While performing any action on a web page using selenium, there is need of locators to perform specific tasks. Locators in web page are used to identify unique elements within a webpage. Web elements could be anything that the user sees on the page, such as title, table, links, buttons, toggle butto
    2 min read
  • get_property() element method - Selenium Python
    Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method – Selenium Python. Just bein
    2 min read
  • How to handle Action class in Selenium?
    Selenium can click buttons, type­ in text boxes, and eve­n scroll through pages, all by itself! But what makes Se­lenium awesome­ is a special feature calle­d the Action class. The Action class lets Se­lenium perform more comple­x actions, like dragging and dropping items, or holding down a key and
    4 min read
  • tag_name element method - Selenium Python
    Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method – Selenium Python. Just bein
    2 min read
  • How to handle multiple windows in Selenium?
    Handling multiple windows in Selenium is crucial when dealing with web applications that open new tabs or windows during interactions. Whether you're testing applications with pop-ups, modals, or new browser tabs, Selenium WebDriver provides efficient methods to switch between multiple windows or ta
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences