Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
I'm trying to invoke the click event of a HTML button that lives inside a WebBrowser control in my Windows Forms application. I have successfully clicked many buttons before(I needed to click a button, which i did, but then their was another button that asked me to confirm an action. and i cant seem to click it!) this button has the same exact HTML as the button prior to it, however i cant seem to click it with the same coding, I don't get an error, just nothing happens. So, my objective: Get to the end target just like a human would. (no direct links, but the links that it would take a human to do it). Next have the program fill in a form, based on my windows form application's textboxes. Next, click a radio-button(bubbled). Click submit, And Finally click confirm. I managed to do all of this except clicking confirm! Im not sure why that is!
OK, here is the code:

private void button5_Click(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Making human-like decisions..."; //update status bar.. 
            toolStripProgressBar1.Value = 50; //update status bar..
            wb.Navigate((boturl.Text) + "/dorf1.php"); //go to Page one
            while (wb.ReadyState != WebBrowserReadyState.Complete)  //wait for loaded page
            {
                Application.DoEvents();
            }
            wb.Navigate((boturl.Text) + "/dorf2.php");  // got to page two
            while (wb.ReadyState != WebBrowserReadyState.Complete)  //wait for loaded page
            {
                Application.DoEvents();
            }
            wb.Navigate((boturl.Text) + "/build.php?id=39"); // go to page three
            while (wb.ReadyState != WebBrowserReadyState.Complete)  //wait for loaded page
            {
                Application.DoEvents();
            }
            wb.Navigate((boturl.Text) + "/a2b.php"); // go to page four, the form that needs to be filled out.
            while (wb.ReadyState != WebBrowserReadyState.Complete) //wait for loaded page
            {
                Application.DoEvents();
            }

            // ok, now we fill out the form

            toolStripProgressBar1.Value = 70; //update status bar..
            toolStripStatusLabel1.Text = "Selecting Troops"; //update status bar..
            HtmlElement xcord = wb.Document.All["xCoordInput"]; //Find text box
            xcord.SetAttribute("value", (textBox1.Text)); //fill text box
            HtmlElement ycord = wb.Document.All["yCoordInput"];  //Find text box
            ycord.SetAttribute("value", (textBox2.Text));  //fill text box
            toolStripProgressBar1.Value = 90; //update status bar..
            toolStripStatusLabel1.Text = "Selecting Coords'"; //update status bar..
            foreach (HtmlElement hem in wb.Document.All)  //Click a bubble
            {
                if (hem.Name == "c" && hem.GetAttribute("value") == "3")
                {
                    hem.InvokeMember("click");
                    break;
                }
            }

            //  more filling out to do..

            HtmlElement unit1 = wb.Document.All["t1"];
            unit1.SetAttribute("value", (textBox4.Text));
            HtmlElement unit2 = wb.Document.All["t2"];
            unit2.SetAttribute("value", (textBox5.Text));
            HtmlElement unit3 = wb.Document.All["t3"];
            unit3.SetAttribute("value", (textBox6.Text));
            HtmlElement unit4 = wb.Document.All["t4"];
            unit4.SetAttribute("value", (textBox7.Text));
            HtmlElement unit5 = wb.Document.All["t5"];
            unit5.SetAttribute("value", (textBox8.Text));
            HtmlElement unit6 = wb.Document.All["t6"];
            unit6.SetAttribute("value", (textBox9.Text));
            HtmlElement unit7 = wb.Document.All["t10"];
            unit7.SetAttribute("value", (textBox10.Text));
            HtmlElement unit8 = wb.Document.All["t9"];
            unit8.SetAttribute("value", (textBox11.Text));
            HtmlElement unit9 = wb.Document.All["t8"];
            unit9.SetAttribute("value", (textBox12.Text));
            HtmlElement unit10 = wb.Document.All["t7"];
            unit10.SetAttribute("value", (textBox13.Text));
            
            //click ok button

            HtmlElement sendraid2 = wb.Document.All["btn_ok"];
            sendraid2.InvokeMember("click");
            while (wb.ReadyState != WebBrowserReadyState.Complete) // wait for loaded page
            {
                Application.DoEvents();
            }

            // <big>Click Confirm - HERE IS THE ISSUE</big>

            HtmlElement sendraid3 = wb.Document.All["btn_ok"];
            sendraid3.InvokeMember("click");
            while (wb.ReadyState != WebBrowserReadyState.Complete) //wait for the loaded page
            {
                Application.DoEvents();
            }
            
            
            toolStripProgressBar1.Value = 100; //update status bar..
            if (toolStripProgressBar1.Value == 100) //update status bar..
            {
                toolStripStatusLabel1.Text = "Sending..."; //update status bar..
                toolStripProgressBar1.Value = 0; //update status bar..
                toolStripStatusLabel1.Text = "Ready To Bot."; //update status bar..
            }
            
            // ok now the above actions will repeat IF there is a value in textbox3. it will repeat the above code every X                    Minutes.

            if (this.textBox3.Text != "")
            {
                int theInterval = 0;
                if (Int32.TryParse(this.textBox3.Text, out theInterval))
                {
                    if (theInterval != 0)
                    {
                        theTimer.Interval = theInterval * 10000;
                        theTimer.Start();
                    }
                }
            }
            else
            {
                btn_StopTimer_Click(null, null);
            }
             
        }




and here is the button's HTML(this is the button i cannot seem to click):
<button type="submit" value="ok" name="s1" id="btn_ok"><div class="button-container"><div class="button-position"><div class="btl"><div class="btr"><div class="btc"></div></div></div><div class="bml"><div class="bmr"><div class="bmc"></div></div></div><div class="bbl"><div class="bbr"><div class="bbc"></div></div></div></div><div class="button-contents">confirm</div></div></button>


Thanks!
Posted
Updated 1-Jul-11 9:07am
v4
Comments
vlad781 30-Jun-11 18:17pm    
I have tried submit, ok, s1, and btn_ok
Sergey Alexandrovich Kryukov 30-Jun-11 18:23pm    
Tag ASP.NET (if there is C# tag, doesn't it mean ASP.NET?). Do it even you got a solution -- this is in your interest, to get relevant answers.
--SA
Philippe Mori 30-Jun-11 19:38pm    
It is not clear what are you trying to do, in which language/framework and if you want to handle the click on the client or server.

Also you code sample is poorly formatted and why all those divs ?
Manfred Rudolf Bihy 1-Jul-11 5:11am    
Yes, changing the title was a good idea too!
You'll have to remember to add more details next time you pose a question. I was able to glean what you were on about because I already had done some development with the WebBrowser Control.
Philippe Mori 1-Jul-11 17:07pm    
It seems like there a missing wb.Navigate betwwen the uses of sendraid2 and sendraid3. I guess the navigation is done by calling InvokeMember("click").

Does your form contains a single button with an id of btn_ok? Have you verified that the proper button was found and not another control like a label with the same id?

Maybe instead of clicking the button, you could manually make the post request with the appropriate information and when the answer is obtained update the content of the browser.
 
Share this answer
 
Comments
vlad781 1-Jul-11 17:16pm    
I don not understand... ):
Philippe Mori 1-Jul-11 17:20pm    
You can make a GET or POST request to a web site by code instead of relying on a browser control to do it.
Sergey Alexandrovich Kryukov 3-Jul-11 1:17am    
This is often a better solution, my 5. Or it can be Ajax request. The idea process the data on server part is usually better then Javascripting things.
--SA
Sergey Alexandrovich Kryukov 3-Jul-11 1:22am    
As to Ajax: I think I see the problem: the button is not used in form submission and onClick is missing. No wonder nothing happens. Please see my answer (with comment).
--SA
I cannot see the form tag. A button should send HTTP request. Is can be down either in the framework of some Web form (in this case all form data will be posted) or via Ajax. You would need to trigger Ajax call in the handler of the button's event onclick; in this case you can do it without a form.

—SA
 
Share this answer
 
Comments
vlad781 1-Jul-11 0:42am    
I have a button on my form that will need to click a button on in the Web Browser.
when i run the above code, the button on the WB is not click. but i dont get an error either.
Sergey Alexandrovich Kryukov 3-Jul-11 1:20am    
Don't say so. If the user clicks a button, it is clicked. But -- wait a second! -- where is your onClick argument?! There is no such thing as miracle: you need to have onClick and handle this event.
--SA
You should ask the web site owner to offer a web service for what you are trying to do ? I would then be much easier to uses.

By the way, are you sure that the owner of the site want you to automatically fill forms like that? I hope it is not a spamming application that you are building...
 
Share this answer
 
Comments
vlad781 1-Jul-11 20:38pm    
nope. I am making a program for a game that requires me to do things regularly, and im going away for the weekend so, i will have the program play the game for me :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900