To get the message you will have to perform the following procedures:
1- Insert a script tag into the head of your webbrowser containing a new function implementation window.alert
, this implementation should add the Alert string to some html element.
2- Perform the procedure for Alert to be displayed and consequently populate the element with the Alert string.
3-Retrieve the string placed in that widget
First Step
Create tag script that will be inserted
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement tagScript = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement bloqAlert = (IHTMLScriptElement)tagScript.DomElement;
Now we define the string representing the contents of the script tag:
You must implement Alert code here, knowing you will receive the Alert message as argument
bloqAlert.text = "window.alert = function (msgAlert) {
//SE A PÁGINA UTILIZAR JQUERY UM EXEMPLO SERIA
$('#elementoUsadoParaReceberAMensagem').val(msgAlert);
}";
Second Step
Add the script tag created to the document head loaded into your webbrowser
head.AppendChild(scriptEl);
Done this if Alert is invoked on the page your message will go to that html element
Third Step
Recovers the string placed in the element
string mensagemAlert = webBrowser1.Document.GetElementById("elementoUsadoParaReceberAMensagem").GetAttribute("value");
You tested my answer?
– Fernando Medeiros
@Diegomoreno I had initially understood that you were only asking about "what to use", so I just quoted Selenium Webdriver and gave no detailed example. Now I updated my reply with more comprehensive details demonstrating how to instantiate the Webdriver, how to navigate to the page that should be accessed by the robot and how to interact with elements, and how to get the text of the Alert message.
– Ulysses Alves
Thank you very much.
– Diego Moreno