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
347 views
in Technique[技术] by (71.8m points)

c# - Click an HTML link inside a WebBrowser Control

C# Visual Studio 2010

I am loading a complex html page into a webbrowser control. But, I don't have the ability to modify the webpage. I want to click a link on the page automatically from the windows form. But, the ID appears to be randomly generated each time the page is loaded (so I believe referencing the ID will not work).

This is the content of the a href link:

<a 

id="u_lp_id_58547" href="javascript:void(0)" class="SGLeftPanelText" onclick="setStoreParams('cases;212', 212); window.leftpanel.onClick('cases_ss_733');return false; ">

My Assigned</a>

Is the anyway to click the link from C#?

Thanks!


UPDATE:

I feel like this is close but it is just not working:

HtmlElementCollection links = helpdeskWebBrowser.Document.Window.Frames["main_pending_events_frame"].Document.GetElementsByTagName("a");
MessageBox.Show(links.Count.ToString());

I have tried plugging in every single frame name and tried both "a" and "A" in the TagName field but just have not had any luck. I can just not find any links; the message box is always 0. What am I missing?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Something like this should work:

HtmlElement link = webBrowser.Document.GetElementByID("u_lp_id_58547")
link.InvokeMember("Click")

EDIT:

Since the IDs are generated randomly, another option may be to identify the links by their InnerText; along these lines.

HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");

foreach (HtmlElement link in links)
{
    if (link.InnerText.Equals("My Assigned"))
        link.InvokeMember("Click");
}

UPDATE:

You can get the links within an IFrame using:

webBrowser.Document.Window.Frames["MyIFrame"].Document.GetElementsByTagName("A");

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

...