填写网页表格 [英] Fill web form

查看:72
本文介绍了填写网页表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从用C#编写的Windows窗体应用程序中输入一些数据到网页,但是我被卡住了.如果有人可以提供帮助,我将不胜感激.该应用程序在另一个选项卡中具有一个表单和一个浏览器.我将数据输入表单,并在提交时调出要输入数据的网页.如果页面未打开,我将使用下面的第一段代码自动登录到该页面,输入我的用户名和密码.这部分效果很好.现在,打开页面后,我尝试了两种不同的方法来尝试将表单数据放入网页中,但它们都无能为力. 没有错误或异常所以我假设代码可以正常工作,但找不到任何东西.希望我能提供足够的信息...
预先感谢任何关注此内容的人!

我添加了一行代码来编写一个文本文件,其中包含我用来尝试查找所需文本框的元素集合的输出结果,这似乎是代码停止的地方.
文本文件仅包含System .__ ComObject大约25行,因此这就是collections数组的全部内容.没有元素,所以现在我知道为什么它停止了.有没有办法做到这一点?还是所有服务器端都无法访问这些字段?


I''m trying to enter some data from a windows form application written c# to a web page and I''m stuck. If anyone can help I would greatly appreciate it. The application has a form and a browser in another tab. I input data to the form and on submit it brings up the web page I want data entered into. If the page is not open I auto log into it using the first block of code below to input my user name and password. This part works great. Now with the page open I tried two different approaches to try and get my form data into the web page and they both fail to do anything. No errors or exceptions so I''m assuming the code is working but its not finding anything. I hope I have given enough information...
Thanks in advance for anyone that looks at this!

I added a line of code to write a text file with the output results of the element collection I was using to try and find the textbox I wanted and this is where the code seems to be stopping.
The text file only contained System.__ComObject for about 25 lines so that''s all I''m getting for the collections array. No elements so now I know why its stopping. Is there a way to do this? Or is this all server side and no way to access those fields?


public object Document
       {
           get { return _ActiveWebBrowser.Document; }
       }

       ///
       /// Load the doc, get the elements, complete the forms values and submit
       ///
       public void LoadDocument(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
       {
           //Reference to "Microsoft HTML Object Library"
           MSHTML.IHTMLDocument3 document = null;
           int maxwait = 10000;
           try
           {
               //Make sure it is loaded and ready
               for(int count=0;count < maxwait;count++)
               {
                   if(_ActiveWebBrowser.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                       break;
               }

               if (_ActiveWebBrowser.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
               {
                   //Get the document
                   document = _ActiveWebBrowser.Document as MSHTML.IHTMLDocument3;

                   //I'm getting elements by name input
                   //but I also tried by tagname
                   MSHTML.IHTMLElementCollection colHTML = document.getElementsByName("frmSearch");

                   //Loop over them to find the ones you want

                   foreach (MSHTML.HTMLInputElement el in colHTML)
                   {
                       //Example gets an input element with name=username
                       if ((el.type == "TextBox") && (el.name == "txtTicketNo"))
                       {
                           //Ticketbox.text is the textbox on my form
                           //but I also tried "12345" in place of it                          //to eliminate any possible issue with my form data
                           el.value = Ticketbox.text;
                       }
                       if (el.type == "btnSearch")
                       {
                           MSHTML.HTMLInputElement btnSubmit = el;
                           btnSubmit.click();
                       }

                   }

               }
               else
                   throw new Exception("Timeout waiting for form object to load.");
           }
           catch
           {
               //Do something
           }
       }



这是我要输入到的页面的视图源



Here''s the view source of the page I''m trying to input to

         <pre lang="xml"><HTML>
          <HEAD>
               <META id="META4" name="META4">
               </META>
               <TITLE id="TITLE5">
               </TITLE>
          </HEAD>
          <BODY bottomMargin="0" leftMargin="0" rightMargin="0" topMargin="0">
               <form id="frmSearch" name="frmSearch">
                    <Table id="Table7" border="0" cellPadding="0" cellSpacing="0" class="case_bodybg" width="100%">
                         <TR id="TR8">
                              <TD id="TD9" class="case_bodybg">
                                   <Table id="Table10" border="0" cellPadding="1" cellSpacing="5" class="case_bodybg">
                                        <TR id="TR11" class="normaltext">
                                             <TD id="TD23" width="120">Ticket No.
                                             </TD>
                                             <TD id="TD24" width="5">
                                             </TD>
*****This is the box I want to fill "txtTicketNo"*******
                                             <TD id="TD25" width="180">
                                                  <input class="textbox_medium" tabIndex="2" maxLength="9" name="txtTicketNo" size="15" type="TextBox" id="txtTicketNo"></input>
                                             </TD>
                                             </TR>
                                        <TR id="TR13" align="Center" class="normaltext">
                                             <TD id="TD292" align="Center" vAlign="Bottom" colSpan="11">
                                                  <Table id="Table293" align="Center">
                                                       <TR id="TR294" align="Center" vAlign="Bottom">
                                                            <TD id="TD287" align="Center" vAlign="Bottom">
                                                                 <img id="btnSearch" align="Center" dataSrc="dsSearchOrder" name="btnSearch" src="../../../images/btn_Find.gif" style="cursor:hand;" ActionEvent="onclick" validateControl="True" validateFunction="ValidateSave" callbackFunction="Order_callBack" gridStylePath="Ticket.xml" ResultSetType="grid" onClick="SavePage">
                                                                 </img>
                                                            </TD>
                                                            <TD id="TD288" align="Center" vAlign="Bottom">
                                                                 <img id="btnClear" align="Center" name="btnClear" src="../../../images/btn_Clear.gif" style="cursor:pointer;" onClick="clearFields(''frmSearch'')">
                                                                 </img>
                                                            </TD>
                                                            <TD id="TD308" align="Center" vAlign="Bottom">
                                                                 <img id="ImgCTSMSearch" name="ImgCTSMSearch" src="../../../images/btn_ctsmsearch.gif" style="cursor:pointer;" onClick="callCTSM();">
                                                                 </img>

推荐答案

在遍历对象以查找文本框的地方,您可能会寻找
Where you loop through your objects in looking for the textbox, you could possibly look for
(el.id == "txtTicketNo"))


代替


instead of

(el.name == "txtTicketNo"))



我也不相信要寻找一种文本框".出于某种原因,这对我来说似乎是错误的,但这可能只是我所经历的一天.



I''m also not convinced of looking for a type of "textbox". For some reason that looks wrong to me, but it may just be the type of day I''m having.


这篇关于填写网页表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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