Check If Any Alert Exists Using Selenium with Python

Check if any alert exists using selenium with python

What I do is to set a conditional delay with WebDriverWait just before the point I expect to see the alert, then switch to it, like this:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
browser.find_element_by_id("add_button").click()

try:
WebDriverWait(browser, 3).until(EC.alert_is_present(),
'Timed out waiting for PA creation ' +
'confirmation popup to appear.')

alert = browser.switch_to.alert
alert.accept()
print("alert accepted")
except TimeoutException:
print("no alert")

WebDriverWait(browser,3) will wait for at least 3 seconds for a supported alert to appear.

How can I click on alert pop up in python selenium

Try this with explicitly wait condition:

alert = WebDriverWait(driver, 30).until(
EC.alert_is_present())

alert.accept()

imports:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

How to understand that a page has an alert or not with use of Selenium in Python?

You can do:

driver.executeScript("window.alert = () => window.alertHappened = true")
// some code here that may cause alert
alert_happened = driver.executeScript("return !!window.alertHappened")

How to check if an alert is present on a page with JavaScript or Selenium

You can disable alerts with:

window.alert = () => false

from python that's:

driver.execute_script("window.alert = () => false")

Selenium Alert Handling Python

JavaScript can create alert(), confirm() or prompt()

To press button OK

driver.switch_to.alert.accept()  # press OK

To press button CANCEL (which exists only in confirm() and prompt())

driver.switch_to.alert.dismiss()  # press CANCEL

To put some text in prompt() before accepting it

prompt = driver.switch_to.alert
prompt.send_keys('foo bar')
prompt.accept()

There is no function to check if alert is displayed

but you can put it in try/except to catch error when there is no alert.

try:
driver.switch_to.alert.accept() # press OK
#driver.switch_to.alert.dismiss() # press CANCEL
except Exception as ex:
print('Exception:', ex)

Minimal working example.

Because I don't know page which displays alert so I use execute_script to display it.

from selenium import webdriver
import time

#driver = webdriver.Firefox()
driver = webdriver.Chrome()

driver.get('http://google.com')

# --- test when there is alert ---

driver.execute_script("console.log('alert: ' + alert('Hello World!'))")
#driver.execute_script("console.log('alert: ' + confirm('Hello World!'))")
#driver.execute_script("console.log('alert: ' + prompt('Hello World!'))")
time.sleep(2)

try:
driver.switch_to.alert.accept() # press OK
#driver.switch_to.alert.dismiss() # press CANCEL
except Exception as ex:
print('Exception:', ex)

# --- test when there is no alert ---

try:
driver.switch_to.alert.accept() # press OK
#driver.switch_to.alert.dismiss() # press CANCEL
except Exception as ex:
print('Exception:', ex)

# ---

driver.save_screenshot('image.png')
driver.close()

BTW: if you want to first try to press CANCEL and when it doesn't work then press OK

try:
driver.switch_to.alert.dismiss() # press CANCEL
except Exception as ex:
print('Exception:', ex)
try:
driver.switch_to.alert.accept() # press OK
except Exception as ex:
print('Exception:', ex)

BTW: different problem can be popup notifications or geolocation alerts

How to click Allow on Show Notifications popup using Selenium Webdriver

Wait until alert is not present - Selenium/Python

You can wait for a specific URL, title, or a specific element to be present or visible, but you can also have a specific alert_is_not_present custom Expected Condition:

class alert_is_not_present(object):
""" Expect an alert to not to be present."""
def __call__(self, driver):
try:
alert = driver.switch_to.alert
alert.text
return False
except NoAlertPresentException:
return True


Related Topics



Leave a reply



Submit