Clicking button automatically using HtmlAgilityPack

前端 未结 4 702
梦如初夏
梦如初夏 2020-12-21 06:07

I want to click a button on a web page automatically. I am using HtmlAgilityPack. I can take the button\'s Xpath. But I could not fire the click event of the bu

相关标签:
4条回答
  • 2020-12-21 06:23

    I tried this using HAP but could not got a solution for clicking an input button. I used simplebrowser which worked like magic to achieve this.

    https://github.com/axefrog/SimpleBrowser

    0 讨论(0)
  • 2020-12-21 06:24

    Html Agility Pack is not supposed to be used to simulate clicks on buttons. It is used only for parsing HTML. If you want to send HTTP requests you could use a WebClient.

    0 讨论(0)
  • 2020-12-21 06:38

    Have a look at the following answer: How to click a link element programmatially with HTMLElement?

    He is creating a HtmlElement object (via xPath or by any other way) and then "invoking" the click event with the code:

    htmlItem.InvokeMember("click");
    
    0 讨论(0)
  • 2020-12-21 06:39

    An option that is a bit slow, but it works:

    HtmlAgilityPack.HtmlNodeCollection ExpanderButtonNodes = Document.DocumentNode.SelectNodes("//div[@class='cd-expand-button']");
       if (ExpanderButtonNodes != null)
          foreach (HtmlAgilityPack.HtmlNode Node in ExpanderButtonNodes)
             foreach (HtmlElement Element in webBrowser1.Document.GetElementsByTagName("div"))
                if (Element.InnerText != null && Node.InnerText.Length > 0 && Element.InnerText.Contains(Node.InnerText))
                   Element.InvokeMember("click");
    

    This could be improved upon but gives a base using two different ways to get to the same thing.

    0 讨论(0)
提交回复
热议问题