如何在C#win表单应用程序中单击Web按钮 [英] How do I outo click web button in C# win form application

查看:49
本文介绍了如何在C#win表单应用程序中单击Web按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在这个网站上这样做

web

< input id =btnSubmitclass =btnvalue =btn>



使用此C#代码

HtmlDocument doc = this.webBrowser1.Document;

webBrowser1.Document.GetElementById(btnSubmit ).InvokeMember(点击);



但我不能这样做

web

< button type =submitclass =btn btn-primary btn-lg> Next< / button>



我尝试了什么:



i想要点击网页按钮自动在C#win表单应用程序中为所有类型的按钮

带有id或没有id

i can do this for this web
web
<input id="btnSubmit" class="btn" value="btn">

using this C# code
HtmlDocument doc = this.webBrowser1.Document;
webBrowser1.Document.GetElementById("btnSubmit").InvokeMember("click");

but i can not to do this for
web
<button type="submit" class="btn btn-primary btn-lg">Next</button>

What I have tried:

i want to click web button Automatically in C# win form application for all typ of button
with id or without id

推荐答案

试试这个



try this

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
           HtmlElementCollection buttons =  webBrowser1.Document.GetElementsByTagName("button");
           if (buttons != null)
           {
               HtmlElement nextButton = buttons.Cast<HtmlElement>().FirstOrDefault(k => k.InnerText == "Next");
               if (nextButton != null)
                  nextButton.InvokeMember("click");
           } 
        }


虽然您使用了'button'类型而不是'input'但是当浏览器自动渲染您的网页时将其转换为输入,检查下面的代码段并查看它是否适合您

Though you have used 'button' type instead of 'input' but when browser render your web page it automatically converts it to 'input', check below snippet and see if it works for you
HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("input");  
foreach (HtmlElement el in elc)  
{  
   if (el.GetAttribute("type").Equals("submit"))  
   {  
        el.InvokeMember("Click");  
   }  
 }



以上代码从HTML源收集标签,以'input'开头,然后检查标签的类型是否提交,如果是。然后它将直接调用click方法。



其他方法是检查视图源并复制web按钮的'id'并在下面的代码中使用它


above code collect a tag from HTML source start with 'input' and then check of the type of the tag is submit, if it is. then it will directly Invoke click method.
OR
Other way is to check the view source and copy the 'id' of your web button and used it in below code

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document
        .GetElementById("HTMLID_of_your_web_button")
        .InvokeMember("Click");
}


这篇关于如何在C#win表单应用程序中单击Web按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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