Prevent, confirm or Dismiss javascript Alerts in a VB.NET webbrowser object

Asked

Viewed 118 times

0

I am facing problems with javascript alerts on my webbrowser.
I’m creating an automation and after giving Ubmit on some pages in webbrowser, Javascript alerts appear with the protocol generated by the page, but with the alert open webbrowser does not load other page via navigate, when the timer sends the command the application breaks because the alert is active.

WebBrowser1.Navigate(paginaInicial)

I tried to use Sendkeys, but without success. In my researches I did not find any effective solution.

SendKeys.Send("{ENTER}")

Dear friends, how I solve this problem?

  • 3
  • https://answall.com/a/246480/69359

  • in the above links, have the two forms, block the Alert to appear and click ok of windows.

  • I had already tried to use a similar solution but without success! This solution is in C#, I don’t know much I’m trying to write this in VB, but I’m facing difficulties.

  • I had already tried to inject in the head, something like "window.Alert = Function () { }", but also unsuccessfully.

1 answer

1


I managed to solve this problem as follows:

The moment I need to take the browser to the home page, Webbrowser1.Navigate(homepage), i did the following... When trying to go to the homepage and the browser is locked with active alert, I apply the method .Dispose() on the object, and yet I define it as = Nothing.
After that I create again a Webbrowser as the same characteristics, but it will have a different nine so I used a Random() to generate an object with a random name.
Since my application only has one Webbrowser, all the places where I make some request for my Webbrowser I need to use a code that searches the object by Ctype(), since I do not know the name of the new Webbrowsers that will be created.

So the code goes like this:

//Tenta redirecionar para página inicial
Try
//Procura o WebBrowser ativo
For Each ctrlsweb In Me.Controls
  If (ctrlsweb.GetType() Is GetType(WebBrowser)) Then
    Dim browserc As WebBrowser = CType(ctrlsweb, WebBrowser)
    //Aqui você executa as ações para esse webbrowser que o codigo transformou em variável
    browserc.Navigate(pagInicial)
  End If
Next

//Se houver algum erro, então e
Catch ex As Exception

//Elimina o objeto Browser ativo 
For Each ctrlsweb In Me.Controls
  If (ctrlsweb.GetType() Is GetType(WebBrowser)) Then
    Dim browserc As WebBrowser = CType(ctrlsweb, WebBrowser)
    browserc.Focus()
    SendKeys.Send("{ENTER}")
    browserc.Dispose()
    browserc = Nothing
  End If
Next

//Gera um número randomico para usar no nome do objeto Browser
Dim geraRand As Random = New Random()
Dim numero As String = geraRand.Next(1013, 8939)

//Cria novo WebBrowser
Dim web As New WebBrowser
web.Name = "WebBrowser" & numero
web.Dock = DockStyle.Fill
web.ScriptErrorsSuppressed = True

End Try

If your Webbrowser has Handler actions as well as mine, for example Documentcompleted, Navigating, etc... you can use after the creation of Webbrowser this code:

AddHandler web.Navigating, Sub()
                             BarStatus.Text = "Carregando..."
                             StatusOK.Visible = True
                             //Mais código aqui...
                           End Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.