无法以编程方式单击网络浏览器中的按钮 [英] Can't programmatically click a button in the web browser

查看:124
本文介绍了无法以编程方式单击网络浏览器中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用Windows窗体应用程序中WebBrowser控件内的HTML按钮的click事件.我之前已经成功单击了许多按钮(我需要单击一个按钮,但是我确实是单击了,但是后来他们又是另一个按钮,要求我确认操作.我似乎无法单击它!)此按钮具有与HTML相同的确切HTML该按钮之前的按钮,但是我似乎无法使用相同的代码单击它,但没有收到错误消息,只是什么也没有发生.因此,我的目标是:像人类一样达到最终目标. (没有直接链接,但是需要人工完成的链接).接下来,让程序根据我的Windows窗体应用程序的文本框填写一个窗体.接下来,单击单选按钮(带气泡).单击提交,最后单击确认.除了点击确认,我设法完成了所有这些工作!我不确定为什么会这样!
OK,这是代码:

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);
            }
             
        }





这是按钮的HTML(这是我似乎无法点击的按钮):





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!

推荐答案

也许您可以单击适当的信息来手动发出发布请求,而不是单击按钮,并且在获得答案后更新浏览器的内容.
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.


我看不到form标记.一个按钮应该发送HTTP请求. Is可以在某些Web表单的框架中关闭(在这种情况下,所有表单数据都将被发布)或通过Ajax.您将需要在按钮事件onclick的处理程序中触发Ajax调用;在这种情况下,您可以不使用表格.

—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


您应该要求网站所有者提供您要尝试做的事情的Web服务吗?这样我将更容易使用.

顺便说一句,您确定站点的所有者希望您自动填写此类表格吗?我希望它不是您正在构建的垃圾邮件应用程序...
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...


这篇关于无法以编程方式单击网络浏览器中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆