【问题标题】:How Do You Set Value of Input Element Programmatically Through CSharp?如何通过 CSharp 以编程方式设置输入元素的值?
【发布时间】:2011-04-28 18:55:06
【问题描述】:

你好 我正在尝试自动化我的 IE 以登录网站,但问题是输入元素没有 HTML ID 属性!例如:

<input type="text" name="user" size="15" value="">

如何编写 C# 程序以在此文本框中插入文本?

谢谢

【问题讨论】:

    标签: c# html webforms autofill browser-automation


    【解决方案1】:

    将以下属性添加到您的输入标签:runat="server"id="someId"

    <input id="user" type="text" size="15" runat="server">
    

    然后你做的服务器端:

    user.Text = "sample text";
    

    然后你可以这样做:

    foreach (Control c in Page.Controls)
    {
        TextBox t = c as TextBox;
    
        if (t != null)
        {
            t.Text = "sample text";
        }
    }
    

    但我不确定如果没有 runat="server" 属性它会起作用

    【讨论】:

    • 谢谢,但我无权编辑网站上的 HTML :(
    • 我认为他只是想发布到另一个不受他控制的网站
    【解决方案2】:

    也许这会有所帮助:-

    • 注意:也请看http://msdn.microsoft.com/en-us/library/2te2y1x6.aspx http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.aspx http://social.msdn.microsoft.com/Search/en-US/?query=mshtml%20tutorial&ac=1

    • 创建一个新项目,如 Windows 窗体应用程序项目,

    • 添加 MSHTML 的引用,即 Microsoft HTML 对象库和 SHDocVw,即 Microsoft Internet Controls,

    • 创建一个函数,其主体有点像,并将其绑定到按钮的点击事件之类的任何东西上:

              /*INTERNET EXPLORER's OBJECT*/
              SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
      ie.Navigate("http://www.example.com/entry"); /*GO TO EXAMPLE.COM*/
              /*WAIT UNTIL THE BROWSER IS READY AND COMPLETELY LOADED*/
      while (ie.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE) 
              {
                 Application.DoEvents();
              }
              mshtml.HTMLDocument doc = ie.Document;
              while (doc.readyState != "complete")
              {
                 Application.DoEvents();
              }
      /*GET ALL THE INPUT ELEMETS IN A COLLECTION*/
      MSHTML.IHTMLElementCollection collection=
              doc.getElementsByTagName("INPUT");
              foreach (mshtml.IHTMLElement elem in collection)
              {
                if (elem.getAttribute("name") != null)
                  {
                    /*IDENTIFY THE INPUT CONTROL BY NAME ATTRIBUTE*/
            if (elem.getAttribute("name").Equals("user"))
                    {/*ENTER USER NAME*/
                     elem.setAttribute("value", "ABC");
            }
          }
      }                           
      

    【讨论】:

      【解决方案3】:

      正如尼科所说:

      <input id="user" type="text" size="15" runat="server">
      

      但你必须尝试:

      user.Value = "sample text";
      

      安装!

      【讨论】:

        【解决方案4】:

        我知道这有点晚了,但这里是 jquery 方法的替代方法。

        我假设 IE 是指 webbrowser 控件。 获得文档后,您可以浏览输入元素。

        类似

        HtmlElementCollection inputs = browser.Document.GetElementsByTagName("input");

        然后循环遍历每个输入。您可以检查输入的名称 input.GetAttribute("name").Equals("user")

        将值插入字段将使用

        input.SetAttribute("value", "MyUserName");

        【讨论】:

          【解决方案5】:

          我猜这不是“使用 C# 以编程方式进行”,但您可以 jQuerify 页面,然后运行一些自定义 javascript 来操作控件的值。如果你使用WebBrowser,你可以调用下面的方法插入脚本。

          string script = "script text";
          WebBrowser.Navigate(script);
          

          j查询代码

          var s=document.createElement('script');
          s.setAttribute('src','http://jquery.com/src/jquery-latest.js');
          document.getElementsByTagName('body')[0].appendChild(s);
          

          自定义代码

          $(document).ready(function(){$('input[type="text"][name="user"]').val('FooBar')});
          

          【讨论】:

            猜你喜欢
            • 2011-08-26
            • 1970-01-01
            • 1970-01-01
            • 2015-12-24
            • 2022-09-27
            • 2020-10-14
            • 2019-06-24
            相关资源
            最近更新 更多