I'm working on a small tool in C# and using .Net MAUI. Part of it is loading a website and parsing a list of items from that site.

I can load and parse the elements, but the site has a "load more" button instead of pages for the item list. Thus, I need to push that button via code until all elements are loaded and hence get the full html for parsing.

Based on what I found online I tried using angleSharp

b = ( IHtmlElement )document.GetElementsByClassName("theClassName").First();
b.DoClick();

as well as the MAUI WebView component

myWebview.EvaluateJavaScriptAsync(@" 
                var button = document.querySelector('.myClassName');
                if (button) {
                    button.click();
                }
            ");

The first one did nothing, the second one never completes. I also looked at HtmlAgilityPack but didn't find anything to invoke a click.

What am I doing wrong?

3 Replies 3

have you tested your select query in the browser debug console?

yes I have testes the JS in the debug console. There it works.

I found the solution. It seems that MAUIs `WebView` only accepts single line commands, so using

await myWebview.EvaluateJavaScriptAsync("document.querySelector('.buttonClass').click();");

works.

As additional info for those who are interested:

  • It returns null while the button is present. When everything is loaded it return the string "null" instead.

  • myWebview.EvaluateJavaScriptAsync("document.documentElement.innerHTML"); returns the HTML after loading everything

  • WebView doesn't need to show to do this, it can be used in the background for loading the website when other libraries don't work

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.