Click an HTML link inside a WebBrowser Control

前端 未结 2 983
悲哀的现实
悲哀的现实 2020-12-03 16:13

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

相关标签:
2条回答
  • 2020-12-03 16:34

    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.

    0 讨论(0)
  • 2020-12-03 16:37

    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");
    
    0 讨论(0)
提交回复
热议问题