Prevent, confirm or dismiss in javascript alerts on a VB.NET webbrowser object

0

I'm experiencing problems with javascript alerts in my webbrowser. I am creating an automation and after submitting to some pages in the webbrowser, javascript alerts with the protocol generated by the page, however with the alert opened the webbrowser does not load other pages via navigate , when the timer sends the command the application crashes because the active alert is

WebBrowser1.Navigate(paginaInicial)

I have tried to use SendKeys, but to no avail. In my searches I found no effective solution.

SendKeys.Send("{ENTER}")

Dear friends, how do I solve this problem?

    
asked by anonymous 20.12.2017 / 13:53

1 answer

1

I was able to solve this problem as follows:

At the moment I need to bring the browser to the homepage, WebBrowser1.Navigate (home page) , I did the following ... When I try to go to the home page and the browser is locked with the active alert, I apply the .Dispose () / strong> in the object, and I still define it as = nothing . After that I create a WebBrowser again as the same characteristics, however it will have a different nine so I used a Random () to generate a randomly named object. As my application only has a WebBrowser, all the places where I make a request of my WebBrowser I need to use a code that searches the object for CType (), since I do not know the name of the new WebBrowsers that will be created. br> So the code looks 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 like mine, for example DocumentCompleted , Navigating , etc ... you can use this code after creating the WebBrowser: br>

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