florentbr / SeleniumBasic

A Selenium based browser automation framework for VB.Net, VBA and VBScript
BSD 3-Clause "New" or "Revised" License
432 stars 197 forks source link

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

Open AnkitKabra20 opened 7 years ago

AnkitKabra20 commented 7 years ago

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

dpgalloway commented 7 years ago

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

AnkitKabra20 commented 7 years ago

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

dpgalloway commented 7 years ago

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:

AnkitKabra20 commented 7 years ago

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

image image

Afterwards getting an error message

image

dpgalloway commented 7 years ago

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.

AnkitKabra20 commented 7 years ago

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

image

Afterwards getting an error message

image

dpgalloway commented 7 years ago

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

dpgalloway commented 7 years ago

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

AnkitKabra20 commented 7 years ago

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

dpgalloway commented 7 years ago

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

AnkitKabra20 commented 7 years ago

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

dpgalloway commented 7 years ago

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

AnkitKabra20 commented 7 years ago

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 commented 7 years ago

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

ramaoutlook commented 2 years ago

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