WebBrowser control - Get element By type?

末鹿安然 提交于 2019-12-18 06:54:58

问题


I need to get a element by type in C# the HTML looks like this:

<button type="submit" class="orangeBtn">Send Invitations</button>

And I want it to be able to where I can invoke("click") it, but it isn't appearing to work in C#. My current code is:

HtmlElement m_SubmitField = m_Browser.Document.All["orangeBtn"];

if (m_SubmitField != null)
    m_SubmitField.InvokeMember("click");

Is there an alternative working way to do this?

This is NOT my server, so I can't edit the HTML or add jquery.

I'm making an automated application to send invites to friends that I want to join, but they made the button without a id or name as seen above, so is there anyway to have it invoke("click") in C# using a different method?

Thanks.


回答1:


You can use GetElementsByTagName.

var elements = m_Browser.Document.GetElementsByTagName("button");
foreach (HtmlElement element in elements)
{
     // If there's more than one button, you can check the
     //element.InnerHTML to see if it's the one you want
     if (element.InnerHTML.Contains("Send Invitations"))
     {
          element.InvokeMember("click");
     }
}



回答2:


i have tried the following code, and was successfull.

HtmlElementCollection htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName("input");
for (int i = 0; i < htmlcol.Count; i++)
            {
                if (htmlcol[i].GetAttribute("value") == "Search")
                {
                    htmlcol[i].InvokeMember("click");
                }
            }

i have tried this code for the following condition

<input type="submit" value="Search">

you can also try for such html tags and i am hopeful that it will work for you... enjoy




回答3:


Well a button with no id or name like that will submit whatever form it is contained within - if you know the HTML, then does the form have an id? If so you can find the form by it's id and call it's submit method.

In fact you would probably be better to find out all the fields of that form and then just POST to it yourself:

var inputs = new NameValueCollection();
inputs.Add("field1", "value1");
inputs.Add("field2", "value2");
inputs.Add("field3", "value3");

System.Net.WebClient Client = new WebClient();
Client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] POSTResultData = Client.UploadValues(postUrl, inputs);


来源:https://stackoverflow.com/questions/5603164/webbrowser-control-get-element-by-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!