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?
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.
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With