webbrowser 控件 SetAttribute 不以编程方式响应

发布于 2024-12-21 03:51:56 字数 586 浏览 3 评论 0原文

我有应用程序,我需要以编程方式将文本添加到某些字段,它在大多数页面中都有效,但在 www.google.com 中,当我尝试但要搜索值时,它不起作用,直到我单击文本区域,然后出现的值

是有什么方法可以解决

我的代码:

HtmlElementCollection el = webBrowser1.Document.All;
             foreach (HtmlElement H in el)
            {

                if (H.GetAttribute("type").Equals("text") )
                    H.SetAttribute("value", sendtext);

            }

我尝试以编程方式单击它,

object obj = H.DomElement;
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
mi.Invoke(obj, new object[0]);

但它不起作用

i have application and i need to add text programmatically to some fields it works in most pages but in www.google.com when i try to but value to search, it did not work until i clicked on the text area then the value appear

is there any way to get around this

my code:

HtmlElementCollection el = webBrowser1.Document.All;
             foreach (HtmlElement H in el)
            {

                if (H.GetAttribute("type").Equals("text") )
                    H.SetAttribute("value", sendtext);

            }

i tried to click on it programmatically

object obj = H.DomElement;
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
mi.Invoke(obj, new object[0]);

also it does not work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2024-12-28 03:51:56

项目 + 添加引用,浏览选项卡并选择 c:\windows\system32\mshtml.tlb(早期 Windows 版本上为 .dll)。这使您可以访问 DomElement 属性返回的本机 COM 接口。因此,您可以像这样干净地编写代码:

var obj = (mshtml.IHtmlElement)H.DomElement;
obj.click();

或者您可以使用 HtmlElement.InvokeMember() 方法稍微不那么干净地编写代码:

H.InvokeMember("click");

使用此技术运行 google 查询的示例表单:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        webBrowser1.Url = new Uri("http://google.com");
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
    }

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
        if (webBrowser1.Url.Host.EndsWith("google.com")) {
            HtmlDocument doc = webBrowser1.Document;
            HtmlElement ask = doc.All["q"];
            HtmlElement lucky = doc.All["btnI"];
            ask.InnerText = "stackoverflow";
            lucky.InvokeMember("click");
        }
    }
}

Project + Add Reference, Browse tab and select c:\windows\system32\mshtml.tlb (.dll on earlier Windows versions). This gives you access to the native COM interface that the DomElement property returns. So you can write your code cleanly like this:

var obj = (mshtml.IHtmlElement)H.DomElement;
obj.click();

Or you can do it a bit less cleanly with the HtmlElement.InvokeMember() method:

H.InvokeMember("click");

A sample form that runs a google query using this technique:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        webBrowser1.Url = new Uri("http://google.com");
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
    }

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
        if (webBrowser1.Url.Host.EndsWith("google.com")) {
            HtmlDocument doc = webBrowser1.Document;
            HtmlElement ask = doc.All["q"];
            HtmlElement lucky = doc.All["btnI"];
            ask.InnerText = "stackoverflow";
            lucky.InvokeMember("click");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文