Search code examples
c#html-agility-packanglesharp

AngleSharp click without ui interface


I am trying to get information by C# and (AngleSharp or HTML Agility Pack) about available schedules from a web page. The problem is that to see what schedules are available on different days, you have to press a "div" (previous, next). So to have one month schedules, I would have to go through and pag page by page. The problem that I find, is that I can not click on the div. In contrast to javascript in Chrome console if I can do it. I have seen that there is a similar response using DoClick on IHtmlElement, but it does not work, I do not change the page. The browser keeps tending the same html in the Document.


Solution

  • Let's first visit what can be done with AngleSharp:

    • Any kind of requests incl. their manipulation (on request, but also before response)
    • General cookie management (and their manipulation, of course)
    • Querying the DOM and perform "simple" actions (e.g., clicking a button, submitting a form)
    • Running trivial JavaScript files

    Here trivial means: Scripts that do not need any capabilities beyond what AngleSharp offers, e.g., rendering tree information, advanced CSSOM access, ... - or scripts that require non-ES5 compliant parsers (e.g., make use of ES6 or some special non-standard capabilities).

    The problem I see in your question description is that in order to "click" a div on a page a script needs to be run. This script can now fall into the "trivial" category, however, most likely it is not. Now you have 2 options:

    • Try it out and maybe it works / great, otherwise ...
    • See what the script is doing (obviously some HTTP request eventually ...) and do the same

    The latter can of course be re-implemented in C# / AngleSharp. So you can create an HTTP request, get the data and either do something on that data set directly (it may be JSON and already what you want ....) or (if it is serving partial HTML) re-parse it and integrate it on the real page.

    HTH!