I am building a console application where I fetch html of a page and parse it using HtmlAgilityPack. After modifying some values of form present on this page I want to submit it by clicking the submit button on the same page. I am able to fetch the button using it's ID attribute but not able to click it programatically. So how should I go about this? Basically I want to submit the form after filling the details so is there any way I can achieve this through my console application.
-
It seems your question here: http://stackoverflow.com/questions/43571355/make-a-button-click-itself/43571424#43571424 – taile Apr 24 '17 at 09:33
-
@PhucTaiLe: I don't think so. (If you want to say that your link should answer OP's question) – Clijsters Apr 24 '17 at 09:41
-
Sorry, I mean the link is answer your question :) – taile Apr 24 '17 at 09:42
-
@PhucTaiLe my question is quite different , I am not making a script and not going to directly paste it in console , I am fetching HTML in my console application and want to perform button click action from there after filling the form. And if I cant perform the click action then what is the work around for this? – vidit mathur Apr 24 '17 at 10:04
-
You don't have to use a WebBrowser. I am not familiar with HtmlAgilityPack but I am sure it is possible. An alternative probably is to use Internet Explorer. You can use Internet Explorer without a window much like HtmlAgilityPack. Since you are now using WebBrowser I assume it is too late to provide alternatives. – Sam Hobbs Apr 24 '17 at 17:41
1 Answers
You can't post an HTML document from a Console Application. fetching the document means that you parsed the HTML text but you are not linked with the Web Server on which the page is running, which would be listening to your postback.
Your only way would be to run a WebBrowser on your console application, navigate to your page and once your navigation happens, retrieve the document text, parse it as you like and then, via the webbrowser, invoke the Click method on your button.
Here is a little sample:
class Program
{
private static WebBrowser wb1 = new WebBrowser();
[STAThread]
static void Main(string[] args)
{
runBrowserThread(new Uri("http://www.google.it"));
}
private static void runBrowserThread(Uri url)
{
var th = new Thread(() => {
var br = new WebBrowser();
br.DocumentCompleted += Br_DocumentCompleted; ;
br.Navigate(url);
Application.Run();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
private static void Br_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Retrieve string content of document
var document = ((WebBrowser)sender).Document;
var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)document.DomDocument;
var content = documentAsIHtmlDocument3.documentElement.innerHTML;
//Parse content with html agility pack or whatever
//Click on button
wb1.Document.GetElementById("myId").InvokeMember("click");
Application.ExitThread();
}
}
Consider i didn't test it but should be a starting point. Also, if you have a complex page you might not want to rely on InternetExplorer (default WebBrowser) to load your page, but might consider using Firefox with GeckoFX, in which case you would have to write the HTML retrieval part in a way a bit different from the IE counterpart.
Remember to include System.Windows.Forms and Microsoft.mshtml if you want to try this sample out

- 174
- 9
-
I tried it and it throws the following exception :- An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll Additional information: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment. @Emanuele Ghetti – vidit mathur Apr 24 '17 at 11:00
-
Oh sure, the old sweet problem of STA-Whatever... I updated the answer with working (and tested, this time :P) code You can find more info about that in this answer http://stackoverflow.com/a/4271581/7581948 @vidit mathur – Emanuele Ghetti Apr 24 '17 at 14:49
-
Obviously you don't know how to post an HTML document from a Console Application but that is not proof that it is not possible. It is possible to navigate to a page using Internet Explorer in an IE window instead of a WebBrowser window. – Sam Hobbs Apr 24 '17 at 17:46
-
Give me a proof that it is possible, using ONLY a Console and simple WebRequests. Also, the default WebBrowser internally uses Internet Explorer so i have no idea what you are talking about – Emanuele Ghetti Apr 26 '17 at 06:16
-
@EmanueleGhetti I have tested your code and it works great. Thank you. I'm new to this, so forgive what is probably a remedial question. I need to click a button in the web page. I have found that .GetElementsById() does not return any elements unless I first show content using `MessageBox.Show(content);` It's as though the page isn't loaded until I do this. This isn't an acceptable solution, since I need unattended operation. Any suggestions? Thank you. – Tony Mar 16 '18 at 18:38