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