Click an HTML link inside a WebBrowser Control

前端 未结 2 987
悲哀的现实
悲哀的现实 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: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");
    

提交回复
热议问题