I've created an application that grabs data from a web page and saves them to a database. This pages sends some __postbacks. After the postback the page loads some data that I need to save. I do not know how to detect this event so I've created a button which grabs the data when pressed by the user.
How can I automate the process so that the user won't have to check for the postback himself?
That is how can I detect the postback event in my webbrowser control?
The webbrowser control has a DocumentCompleted event. You can use the WebBrowser.Document property to look for something specific in this event.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
if (webBrowser1.Document.Body.InnerHtml.Contains("some way that I know I am ready"))
}
Related
I have a C# 4.0 WinForms application, which has a WebBrowser control and 2-buttons.
Clicking the first button sends a URL to the browser to navigate to a specified webSite.
Clicking the second button parses the OuterHtml of the webBrowser1.Document, looking for an "https://..." link for File Download.
The code then uses a webClient.DownloadFileAsync to pull down a file for further use in the application.
The above code successfully works, if I manually click those buttons.
In an effort to automate this for the end-user, I place the first button's click event, i.e. btnDisplayWeb.PerformClick(); in the form's Form1_Load event. This also works, allowing the webBrowser1 to populate its Document with the desired webSite.
However, I am unable to programatically click the 2nd button to acquire the web link for file download.
I have tried to place the 2nd buttons click event within the browser's DocumentCompleted event, as shown below.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
btnMyUrl.PerformClick();
}
However, from what I've read on StackOverFlow and other sites, it's possible that this particular event gets called more than once, and hence it fails.
I've also attempted to loop for a number of seconds, or even use a Thread.Sleep(xxxx), but the browser window fails to populate until the sleep or timer stops.
I attempted to use the suggestions found on the following StackOverFlow site shown below.
How to use WebBrowser control DocumentCompleted event in C#?
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string url = e.Url.ToString();
if (!(url.StartsWith("http://") || url.StartsWith("https://")))
{
// in AJAX
}
if (e.Url.AbsolutePath != this.webBrowser.Url.AbsolutePath)
{
// IFRAME
}
else
{
// REAL DOCUMENT COMPLETE
}
}
However, in parsing the OuterHtml, nothing is returned in the first two sections, and in the third section, other elements are returned instead of the desired "https://..." link for File Download.
Interestingly, if I use a webBrowser1.ReadyState event, as shown below, and place a MessageBox inside DocumentCompleted, this seems to allow the browser document to complete, because after clicking the OK button, the parsing is successful.
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
MessageBox.Show("waiting", "CHECKING");
btnMyUrl.PerformClick();
}
However, I then have the difficulty of finding a way to click the OK button of the MessageBox.
Is there another event that occurs after the DocumentCompleted event.
OR, can someone suggest how to programmatically close the MessageBox?
If this can be done in code, then I can perform the buttonClick() of the 2nd button in that section of code.
After finding that the addition of a MessageBox allows the webBrowser1.Document to complete, and using webBrowser1.ReadyState event within the webBrowser_DocumentCompleted event, all I needed to do, was to find a way to programmatically close the MessageBox.
Further searching on StackOverFlow revealed the following solution on the site below.
Close a MessageBox after several seconds
Implementing the AutoClosingMessageBox, and setting a time interval, closed the MessageBox and allowed my button click, i.e. btnMyUrl.PerformClick(); to successfully parse the OuterHtml and now the code works properly.
Hopefully, if someone else discovers that placing a MessageBox within the webBrowser_DocumentCompleted event allows the document to complete; the aforementioned AutoClosingMessageBox will assist them as well.
I'm trying to detect when the user clicks on a hyperlink in a WebView control from Windows 10. Is there any way to detect this event?
If you want to catch the click itself you'll need to do that from inside the web page's JavaScript. Depending on where the page comes from you may be able to inject JavaScript code to do so with WebView.InvokeScriptAsync
If you want to detect the navigation triggered by the click then you can handle the WebView.NavigationStarting or NavigationCompleted events.
On Windows 10 I would use the WebView.NewWindowRequested event:
private void WebView1_NewWindowRequested(WebView sender, WebViewNewWindowRequestedEventArgs args)
{
Debug.WriteLine(args.Uri);
args.Handled = true; // Prevent the browser from being launched.
}
I can open the link in my standard browser with this code:
public void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
//cancel the current event
e.Cancel = true;
//this opens the URL in the user's default browser
Process.Start(e.Url.ToString());
}
But the problem is that IE only should be opened when a link on the webbrowser is clicked. When using this code IE also opens when I change the documenttext.
My suggestion would be to take a different approach. At the point in time immediately after the initial page has loaded in the WebBrowser control (Navigated event), you can use the webBrowser1.Document property to retrieve an HtmlDocument instance.
From this you should be able to find your link by using, for example,
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementbyid(v=vs.110).aspx
Then you can add an event handler to detect when this link is clicked, and in this handler, run your code to start the IE process.
I'm using JeffWilcox QR control (QR Scanner), but I have one issue with this control. When I launched a page with this control, it's working all the time, even I click back button and I am in main menu. Do you know how can I handle situation like: if user click back button the QR Scanner control will stop working ?
Working:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
nameOfControl.StopScanning();
base.OnNavigatingFrom(e);
}
When you're navigating through pages, there are some events triggered when you enter or exit a page, such as OnNavigatedFrom (when you left the page) or OnNavigatingFrom (just before leaving the page) or OnNavigatedTo (when you entered a page).
You can find more information about page navigation events in the documentation.
You can take advantage of these events to solve your issue. As it seems, create a handler for the OnNavigatingFrom event in the page where you have the QR control, to stop it.
I have a WebBrowser object in a WPF Page and I'm trying to do something whenever the user interacts with the page. I have intially tried to use the events associated with the WebBrowser object but they don't seem to be firing. Below is a simplified example of what my code is trying to do:
webBrowser.MouseDown += new MouseButtonEventHandler(webBrowser_MouseDown);
with the event handler as:
void webBrowser_MouseDown(object sender, MouseButtonEventArgs e)
{
System.Windows.MessageBox.Show("Pressed");
}
However when I run the page and click inside the WebBrowser no message box is displayed.
Apologies, originally I had mentioned that it was a System.Controls WebBrowser rather than a Forms browser.
Mouse events are not supported by the WebBrowser control, according to the documentation. You need to hook up handlers to the DOM events provided by the document being displayed in the control, using the WebBrowser.Document property. This post has an example of how to do this.
Add the ms html com library
Once the WebBrowser.LoadCompleted event fires try this:
mshtml.HTMLDocumentEvents2_Event doc = ((mshtml.HTMLDocumentEvents2_Event)Browser.Document);
doc.onmouseover += new mshtml.HTMLDocumentEvents2_onmouseoverEventHandler(doc_onmouseover);
or use some other event.
Hope this helps someone.