Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium 2.4.0, how to check for presence of an alert

Tags:

ruby

selenium

Before using selenium 2.4.0 I had the following code working:

alert = page.driver.browser.switch_to.alert
if alert.text
  ....
end

Selenium 2.4.0 contains the change "* Raise in switch_to.alert when no alert is present.", so I get a No alert is present (Selenium::WebDriver::Error::NoAlertOpenError) exception.

How can I check for the presence of an alert with selenium-web-driver 2.4.0?

like image 650
gucki Avatar asked Aug 16 '11 10:08

gucki


People also ask

How do you check if there is an alert exists in Selenium?

New Selenium IDE We can check if an alert exists with Selenium webdriver. An alert is created with the help of Javascript. We shall use the explicit wait concept in synchronization to verify the presence of an alert. Let us consider the below alert and check its presence on the page.


2 Answers

I implemented a method to handle this in Ruby that I think is pretty clean:

def alert_present?
  begin
    session.driver.browser.switch_to.alert
    puts "Alert present! Switched to alert."
    true
  rescue
    puts "No alert present."
    return false
  end
end
like image 173
aceofbassgreg Avatar answered Sep 19 '22 15:09

aceofbassgreg


Here is one option:

driver.switch_to.alert.accept rescue Selenium::WebDriver::Error::NoAlertOpenError

This will click OK on the alert if one is present, else it will fail gracefully (silently).

like image 30
Daniel Avatar answered Sep 19 '22 15:09

Daniel