6

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?

2 Answers 2

17

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"); 
Sign up to request clarification or add additional context in comments.

6 Comments

but the question says "But, the ID appears to be randomly generated each time the page is loaded (so I believe referencing the ID will not work)."
John, you are correct -- the links get a random number assigned to them each time you open the page. This method is what I keep coming across in my searches but I can't figure out another way to implement it without using the ID. The 'cases_ss_733' is always the same, I just don't know how that could be used.
@John. Yes you're right, but the randomly generated id portion was added after my initial response. @Josh. Could you identify the link by the InnerText value?
I believe that has me really close Garett! Thank you for following up. I tested it and I did not work but I believe that's because the My Assigned link is buried within an iFrame. I tried inserting a MessageBox.Show(link.InnerText) above the if statement and nothing came up. So then I did a View Source on the frameset page and ran a find for <a and nothing came up. So that makes sense but now the question is, can I get to the link that's embedded in an iFrame. (and thank you so much for the help!)
THANK YOU THANK YOU THANK YOU! I finally figured out it was buried under and additional frame. Once I got drilled down into the second level of frames I found the My Assigned link. Thanks again Garett!
|
1

Perhaps you will have to isolate the link ID value using more of the surrounding HTML context as a "target" and then extract the new random ID.

In the past I have used the "HtmlAgilityPack" to easily parse "screen-scraped" HTML to isolate areas of interest within a page - this library seems to be easy to use and reliable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.