How to capture the alert message using the web browser controler?


I'm developing a robot using Asp.Net C # web browser controler.

In this robot I need to identify some messages that appear in Javascript Alert.

The site that Robot is manipulating is not mine, so there's no way I can make any changes in the structure of it.

So, do you have any way to get Alert content?

asked by anonymous 09.07.2015 / 15:08

2 answers


Perhaps the best alternative for you is to use Selenium Web Driver

With it you can manipulate your browser and have access to all elements of the DOM, allowing you to simulate a user browsing the site, and you can also execute JavaSCript commands, access CSS, etc.

Below is a basic example of how to get the text of an alert using the Selenium Webdriver:

using NUnit.Framework;
using OpenQA.Selenium.Firefox;

namespace ExemploSeleniumWebDriver
    // A classe robo deve ser uma classe de teste para ser executado automaticamente.
    public class Robo
        // As ações do robô são implementadas como testes unitários.
        public void LeiaMensagemDeAlert()
            // Usei aqui o driver do Firefox, mas poderia ser de qualquer outro browser.
            FirefoxDriver driver = new FirefoxDriver();

            // Navega para a página que criei para simular uma mensagem de alert.

            // Clica no botão.

            // Obtém o texto do Alert.
            string textoDoAlert = driver.SwitchTo().Alert().Text;

            // Fecha a janela do alert.

            // Digita a mensagem do alert no campo de texto da tela.
            driver.FindElementById("campoDeTexto").SendKeys("O alert continha o seguinte texto: " + textoDoAlert);

Content of the TestSeleniumWebDriver.html file:

    <title>Exemplo do Selenium Web Driver</title>
    <input name="botaoDeAlerta" type="button" value="Mostrar Alert" onclick="alert('Teste de obtenção da mensagem de alerta via C#.')" />

    <br />

    <label>Selenium WebDriver irá preencher essa caixa de texto com o texto do alerta:</label>
    <br />
    <input type="text" id="campoDeTexto" />
17.07.2015 / 15:32

To get the message you will have to do the following:

1 - Insert a script tag into the head of your webbrowser containing a new implementation of the window.alert function, this implementation should add the alert string to some element of the html.

2 - Perform the procedure so that the alert is displayed and consequently the element with the alert string is popular.

3- Retrieve the string placed on this element

First Step

Create tag script to be inserted

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement tagScript = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement bloqAlert = (IHTMLScriptElement)tagScript.DomElement;

Now we will define the string representing the contents of the script tag:

You should implement the alert code here, knowing that you will receive the alert message as an argument

bloqAlert.text = "window.alert = function (msgAlert) {

Second Step

Adds the created script tag to the head of the loaded document in your webbrowser


If the alert is invoked on the page your message will go to this html element

Third Step

Retrieves the string placed in the element

string mensagemAlert = webBrowser1.Document.GetElementById("elementoUsadoParaReceberAMensagem").GetAttribute("value");
19.07.2015 / 11:21