1

I'm having trouble my application which is supposed to write text in some textboxes on a website and click OK button. But then the problem comes, the site loads a new page and you have to confirm the previous commands by clicking a new OK button. What I need help with is for my C# code to execute the commands as they are called and no do them once the running thread is dead.

Example of my code:

private void startScript_Click(object sender, EventArgs e)
{

     webBrowser1.Document.GetElementById("login_box").InnerText = "NAME";
     webBrowser1.Document.GetElementById("password_box").InnerText = "PASS";

     webBrowser1.Document.GetElementById("login_button").InvokeMember("click");

     //This is where i want the first texts and click to be submitted so it then
     //can click on the confirm button the the next page

     webBrowser1.Document.GetElementById("confirm_login").InvokeMember("click");

     //Submit the confirm click

} //This is where all the information given in the code is actually given/executed to the browser

So to clear up the question. What I need to do is; give the information to the browser directly when the webBrowser1.Document...; is called. A command to submit would also work (webBrowser1.SubmitData) or something like that.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pepps
  • 103
  • 1
  • 1
  • 4
  • Maybe you could use the Navigated event on the WebBrowser class? Otherwise you might want to think about executing the call to the website directly, not throughout the actual web page. I. e. create a WebRequest with either a POST or GET depending on how the webpage works. And submitting the parameters in the request. Hard to tell by the information you are giving. – Håkan Fahlstedt Jan 18 '14 at 10:54
  • Try `MessageBox.Show("Ok to continue")` after you've simulated the click (i.e. at `//Submit the confirm click`). If that works, your main thread's message pump is blocked somewhere after `startScript_Click` has been called. – noseratio Jan 19 '14 at 04:36
  • @Noseratio WOW, thanks man! It worked. But is there any other option as i don't really like to have a confirmation window to show up each time the script runs. I want it completely automated – Pepps Jan 19 '14 at 14:33
  • @Noseratio Sorry, didn't quite understand what you meant before posting, so i should reform my question. How do i unblock the message pump? is my new question – Pepps Jan 19 '14 at 17:03

1 Answers1

0

The WebBrowser navigation, which is taking place when you click the button, is an asynchronous operation, it doesn't complete instantly.

I can't guess what your code is doing immediately after InvokeMember("click"), but if you're trying to access WebBrowser.Document right away, or doing something like Thread.Sleep, that won't work. It worked when followed with MessageBox.Show, because MessageBox.Show runs its own modal message loop.

You need to handle WebBrowser.DocumentCompleted first and then access the document. There are a few ways of doing that, here is one of them.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486