julia
julia

Reputation: 71

Clicking button automatically using HtmlAgilityPack

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 button. How can I do this? Please help.

Upvotes: 5

Views: 24445

Answers (4)

Rusty Nail
Rusty Nail

Reputation: 2710

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.

Upvotes: 1

user2046459
user2046459

Reputation: 21

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

Upvotes: 2

Puce
Puce

Reputation: 1021

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");

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

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.

Upvotes: 8

Related Questions