SeleniumBasic icon indicating copy to clipboard operation
SeleniumBasic copied to clipboard

How to Handle the Alert on Chrome Using Selenium Basic. Getting Run Time Error

Open AnkitKabra20 opened this issue 8 years ago • 15 comments

Hello Users,

Please help me out in getting out of this trouble. I am getting an run time error when alert appears

image

'On Error Resume Next driver.Start "chrome", "http://www.google.com" driver.Manage.DeleteAllCookies 'driver.Window.Maximize

driver.SetCapability "unexpectedAlertBehaviour", "ignore" driver.Get "http://the-internet.herokuapp.com/javascript_alerts" driver.FindElementByCss("#content > div > ul > li:nth-child(3) > button").Click Set dlg = driver.SwitchToAlert(Raise:=False)

Un-commenting On Error Resume Next handles the popup but alert cannot be handled.

On Error Resume Next driver.Start "chrome", "http://www.google.com" driver.SetCapability "unexpectedAlertBehaviour", "ignore" driver.Get "http://the-internet.herokuapp.com/javascript_alerts" driver.FindElementByCss("#content > div > ul > li:nth-child(3) > button").Click Set dlg = driver.SwitchToAlert(Raise:=False)

Assert.False dlg Is Nothing, "No alert present!" Assert.Equals "I am a JS Confirm", dlg.Text

' Close alert dlg.Accept

driver.SwitchToAlert.Accept

AnkitKabra20 avatar Jan 24 '17 17:01 AnkitKabra20

Here's what I have, which works for my set up. For me the error number for alerts is 26, but maybe you need to change to 13.

On Error GoTo Handler Handler: If Err.Number = 26 Then driver.SwitchToAlert().Accept End If

dpgalloway avatar Jan 25 '17 16:01 dpgalloway

I tried but it didn't work. It got stuck and doing nothing. What is your chrome version ?

Public Function Host_Test_App()

'****************************************************************** ' FUNCTION FOR BROWSING THE APPLICATION '****************************************************************** On Error Goto Handler driver.Start "chrome", "http://www.google.com" driver.Manage.DeleteAllCookies 'driver.SetCapability "unexpectedAlertBehaviour", "ignore" driver.Get "http://the-internet.herokuapp.com/javascript_alerts" driver.FindElementByCss("#content > div > ul > li:nth-child(1) > button").Click

Handler: If Err.Number = 26 Then driver.SwitchToAlert().Accept End If Resume Next

End Function

AnkitKabra20 avatar Jan 25 '17 16:01 AnkitKabra20

Oh sorry I also have a Continue: at the end. So:

On Error GoTo Handler Handler: If Err.Number = 26 Then driver.SwitchToAlert().Accept End If Continue:

dpgalloway avatar Jan 25 '17 16:01 dpgalloway

Tried with Continue it did not work either. Its not doing any thing

image image

Afterwards getting an error message

image

AnkitKabra20 avatar Jan 25 '17 16:01 AnkitKabra20

Hmm. Put the "On Error GoTo Handler" right above the "Handler:" line. And also add another step after the Continue: line. It can be anything, like just clicking somewhere on the page.

dpgalloway avatar Jan 25 '17 16:01 dpgalloway

Tried By Putting additional line right after continue. Its doing nothing just stuck image

image

Afterwards getting an error message

image

AnkitKabra20 avatar Jan 25 '17 16:01 AnkitKabra20

Put "On Error GoTo Handler" right above "Handler:" and right under the "....Click" line

dpgalloway avatar Jan 25 '17 16:01 dpgalloway

If it's at the top like that it's not going to be called after the .Click step

dpgalloway avatar Jan 25 '17 16:01 dpgalloway

Please let me know if the code should be like this

Public Function Host_Test_App()

'****************************************************************** ' FUNCTION FOR BROWSING THE APPLICATION '******************************************************************

driver.Start "chrome", "http://www.google.com" driver.Manage.DeleteAllCookies 'driver.SetCapability "unexpectedAlertBehaviour", "ignore" driver.Get "http://the-internet.herokuapp.com/javascript_alerts"

On Error GoTo Handler Handler: driver.FindElementByCss("#content > div > ul > li:nth-child(1) > button").Click If Err.Number = 26 Or 13 Then driver.SwitchToAlert.Accept End If Continue: MsgBox driver.FindElementById("result").Attribute("innerHTML")

' Display alert

End Function

AnkitKabra20 avatar Jan 25 '17 17:01 AnkitKabra20

Do it this way:

Public Function Host_Test_App()

'****************************************************************** ' FUNCTION FOR BROWSING THE APPLICATION '******************************************************************

driver.Start "chrome", "http://www.google.com" driver.Manage.DeleteAllCookies 'driver.SetCapability "unexpectedAlertBehaviour", "ignore" driver.Get "http://the-internet.herokuapp.com/javascript_alerts" driver.FindElementByCss("#content > div > ul > li:nth-child(1) > button").Click

On Error GoTo Handler Handler: If Err.Number = 26 Then driver.SwitchToAlert.Accept End If Continue: MsgBox driver.FindElementById("result").Attribute("innerHTML")

' Display alert

End Function

dpgalloway avatar Jan 25 '17 17:01 dpgalloway

I did the same way you told. Its stuck and not doing any thing. and got the time out image

AnkitKabra20 avatar Jan 25 '17 17:01 AnkitKabra20

Your missing parentheses on SwitchToAlert. I'm not sure if that matters but try this:

Public Function Host_Test_App()

'****************************************************************** ' FUNCTION FOR BROWSING THE APPLICATION '******************************************************************

driver.Start "chrome", "http://www.google.com" driver.Manage.DeleteAllCookies 'driver.SetCapability "unexpectedAlertBehaviour", "ignore" driver.Get "http://the-internet.herokuapp.com/javascript_alerts" driver.FindElementByCss("#content > div > ul > li:nth-child(1) > button").Click

On Error GoTo Handler Handler: If Err.Number = 26 Then driver.SwitchToAlert().Accept End If Continue: MsgBox driver.FindElementById("result").Attribute("innerHTML")

' Display alert

End Function

dpgalloway avatar Jan 25 '17 17:01 dpgalloway

It doesn't matter as it results the same. Are you making it done on your system. What version of chrome browser and extension you are using. May be I should look into that from that perspective.

AnkitKabra20 avatar Jan 25 '17 17:01 AnkitKabra20

Issue is Not with the Code but with the Chrome Driver. As from my org Stand Point Fire Fox Versions are blocked to 52.0 so I was getting the following error when launching the chrome image

This Problem got fixed if I switched the for the older chromedriver version i.e 2.24 from https://chromedriver.storage.googleapis.com/index.html

Alert Issue also got resolved.

Driver.SwitchToAlert().Accept

is working now

AnkitKabra20 avatar Jan 27 '17 19:01 AnkitKabra20

Brother, driver.switchtoalert().accept is not working in this below scenario of sending telegram message to unsaved number.(seleniumTelegramAlertScreenshot) Please help me

ramaoutlook avatar Jun 27 '22 16:06 ramaoutlook