Skip to main content

Selenium + Python + UnexpectedAlertPresentException: Dealing with annoying alerts

Handling UnexpectedAlertPresentException 

Alerts who hates them? I Do! Who doesn't hate an annoying alert causing your tests / scraping job to fail?







UnexpectedAlertPresentException

I must say they are pretty much on point on the Unexpected part! Fortunately, there are easy ways to mitigate the issue.

1. Disable alerts completely:


driver.execute_script('window.alert = function(){};');

execute this script just before where you anticipate the alert and you're golden.


2. You want to see the alert text but not disturb the execution flow.



driver.execute_script('window.alert = console.info;');

Now the alerts have been redirected to the console and you don't have to worry about them. (Unless you have to - then you'd have to monitor the console)


3. You know exactly when it comes and want to accept the alert and move on.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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

try:
    WebDriverWait(browser, 3).until(EC.alert_is_present(),
                                   'Timed out waiting for popup to appear.')
    alert = browser.switch_to.alert
    alert_text = alert.text
    alert.accept()
    print("alert accepted, it was saying: ", alert_text)
except TimeoutException:
    print("No alert caught")



Now What?



You have been just exposed to a new universe of opportunity! There are no limits to what you could do with web scraping. Most startups and existing big businesses do it, and it's not going out of business.

You can build apps around real customer data, get new acquisitions validate your users automate routine tasks list goes on.

You can further explore our blog for interesting reads OR - you can contact us to learn a bit more over a FREE personal Skype coaching session. Just click on "Leave a message" and reach out to us. We get a lot of volume these days so FREE Sessions won't be here for a long time, Grab this opportunity while you can!

Comments

  1. Viksto, Thats a great one! I am going to implement this as soon I can.

    ReplyDelete

Post a Comment

Popular posts from this blog

4. Lex and Yacc Program to detect errors in a 'C' Language Program

Lex and Yacc Program to detect errors in a 'C' Language Program   Lex Code : %{ #include"y.tab.h" #include<stdio.h> int LineNo = 1 ; %} identifier [ a - zA - Z ][ _a - zA - Z0 - 9 ]* number [ 0 - 9 ]+|([ 0 - 9 ]*\.[ 0 - 9 ]+) %% main \(\) return MAIN ; if return IF ; else return ELSE ; while return WHILE ; int | char | flaot return TYPE ; { identifier } return VAR ; { number } return NUM ; \> | \< | \<= | \>= | == return RELOP ; [\ t ] ; [\ n ] LineNo ++; . return yytext [ 0 ]; %% Yacc Code : %{ #include<string.h> #include<stdio.h> extern int LineNo ; int errno = 0 ; %} % token NUM VAR RELOP % token MAIN IF ELSE WHILE TYPE % left '-' '+' % left '*' '/' %% PROGRAM : MAIN BLOCK ; BLOCK : '{' CODE '}' ; CODE : BLOCK | STATEMENT CODE | STATEMENT ; STATEMENT : DECST ';' | DECST { printf ( "\nLine number %d...

2. Lex program that detects statement type i.e. Simple or Compound

Lex program that detects statement type i.e. Simple or Compound Note: Only AND | OR | BUT conjunctions are supported. Program: % option noyywrap %{ char test = 's' ; %} %% ( "" [ aA ][ nN ][ dD ] "" )|( "" [ oO ][ rR ] "" )|( "" [ bB ][ uU ][ tT ] "" ) { test = 'c' ;} . {;} \ n return 0 ; %% main () { yylex (); if ( test == 's' ) printf ( "\n Its a simple sentence" ); else if ( test == 'c' ) printf ( "\n This is compound sentence" ); }