C#如何从程序发送ENTER?

时间:2011-06-23 13:47:07

标签: c# browser sendkeys

我是初学者,我写了这段代码,无法从C#上的webbrowser组件发送ENTER点击:

private void start_submit_Click(object sender, EventArgs e)
{
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load("settings.xml");             
    foreach (System.Xml.XmlNode node in xdoc.SelectNodes("//site"))
    {
        foreach (System.Xml.XmlNode child in node)
        {
                if (child.Attributes["todo"].Value.ToString() == "navigate")
                {
                    navigate(url_string);
                }
                else if (child.Attributes["todo"].Value.ToString() == "checkbox")
                {
                    HtmlElementCollection es = webBrowser1.Document.GetElementsByTagName("input");
                    HtmlElement ele0 = es[1];
                    ele0.SetAttribute("checked", "true");
                }
                else if (child.Attributes["todo"].Value.ToString() == "pressbutton")
                {
                    HtmlElementCollection es = webBrowser1.Document.GetElementsByTagName("input");
                    if (es != null && es.Count != 0)
                    {
                        HtmlElement ele = es[5];
                        ele.ScrollIntoView(true);
                        ele.Focus();
                        SendKeys.Send("{ENTER}");
                   }                           
                }
                else if (child.Attributes["todo"].Value.ToString() == "wait")
                {
                    MessageBox.Show("waiting...");
                 }
        }
    }
}

和settings.xml文件:

<?xml version="1.0" encoding="utf-8" ?>
  <data>
    <site num="1" name="http://www.clantemplates.com" pr="5">
    <step num="1" todo="navigate" value_todo="http://www.clantemplates.com/forums/register.php" value_2_todo="none">navigating</step>
    <step num="2" todo="checkbox" value_todo="true" value_2_todo="8">checking checkbox</step>
    <step num="3" todo="pressbutton" value_todo="enter" value_2_todo="9">pressing button</step>
    <step num="4" todo="wait" value_todo="" value_2_todo="">waiting for other click</step>
  </site>
</data>

没有这件作品,ENTER成功发送

else if (child.Attributes["todo"].Value.ToString() == "wait")
{
    MessageBox.Show("waiting...");
}

有片 - 没有

如何解决?

谢谢!

1 个答案:

答案 0 :(得分:2)

解决了,我这样做:

HtmlElementCollection es = webBrowser1.Document.GetElementsByTagName("input");
HtmlElement ele = es[5];
ele.InvokeMember("Click");