Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well if you were working with the site developers to create a solution for your application, then you would use <code>ObjectForScripting</code> to communicate between JavaScript and the app. There a good article <a href="http://www.dotnetfunda.com/articles/article840-working-with-webbrowser-in-wpf-.aspx" rel="nofollow noreferrer">here</a>, and another question which might be helpful <a href="https://stackoverflow.com/questions/3694028/invoke-c-code-from-javascript-in-a-document-in-a-webbrowser">here</a>.</p> <p>However as I understand your question, the site is an arbitrary third party site with no affiliation to your application, and you want to auto-fill some form values and submit the form within your code.</p> <p>To do this you can handle the <code>LoadCompleted</code> event of the WebBrowser. This is called when the loaded document <code>readyState</code> is changed to completed. You can therefore use this event as a hook to then set/read the document form values. Note you will need to add a reference to <code>Microsoft mshtml</code> in the project. </p> <p>The following is an MVVM style (PRISM) command, which allows an event to bind directly to the <code>ViewModel</code> using behaviors. This is equivalent to registering an event handler in code-behind.</p> <pre><code>public ICommand LoadCompleted { get { return new EventToCommandWithSender&lt;NavigationEventArgs&gt;( (s,e) =&gt; { WebBrowser browser = (WebBrowser) sender; // false if nested frame if (e.IsNavigationInitiator) { mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)browser.Document; // always completed var readyState = doc.readyState; // populate form var name = doc.body.document.getElementById("username"); name.value = "@TheCodeKing"; // submit form var submit = doc.body.document.getElementById("submit"); submit.Click(); } }); } } </code></pre> <p>Unfortunately the <code>NavigationEventArgs</code> don't provide a way to access the HTML document or request data. It does contain a <code>WebRequest</code> property but this has not been implemented, and will always be null. In my example I've assumed a custom <code>EventToCommandWithSender</code> class which providers the sender as well as the event ARGs when the event fires, but it's down to your own implementation to get access to the sender.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload