Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

c# - WebBrowser control - Get element By type?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
     }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...