0

I am trying to programatically execute javascript on my WebView.

Here is my code:

private async void wbView1_Navigated(object sender, WebNavigatedEventArgs e)
{
    var result = await wbView1.EvaluateJavaScriptAsync("document.getElementByClassName('load_more_btn ib');");
    await Task.Delay(10);
}

And here is the HTML from the page:

<div class="center ib-parent">
     <a data-load="loadmore_category" href="#" class="load_more_btn ib">Load more</a>
</div>

It is a "Load More" button which actually loads more content and gives me access to what kind of inforormation I need. Now obvisouly, EvaluateJavaScriptAsync is not doing the trick here. It is not clicking the button or executing the javascript necessary to load more content. Is this the correct way to "click a button" programatically in a WebView?

Alejandro
  • 308
  • 2
  • 11
  • if that a fragment of the HTML or the whole thing? The js you posted just gets a reference to the element, it doesn't actually do anything with it. – Jason Feb 25 '21 at 22:32
  • That is a fragment indeed. How am I supposed to click it then? – Alejandro Feb 25 '21 at 22:33
  • https://stackoverflow.com/questions/902713/how-do-i-programmatically-click-a-link-with-javascript – Jason Feb 25 '21 at 22:35
  • I am on Xamarin forms not android. C# – Alejandro Feb 25 '21 at 22:36
  • 1
    the link I posted has absolutely nothing to do with Android. This is purely a JS issue – Jason Feb 25 '21 at 22:38
  • @Jason you were right, got this working. var result = await wbView1.EvaluateJavaScriptAsync("document.getElementsByClassName('load_more_btn ib')[0].click();"); – Alejandro Feb 25 '21 at 23:00

1 Answers1

0

Ok, thanks to Jason I managed getting some good directions and got it working. Acutally, getElementsByClassName "Returns an array-like object of all child elements". Since it is an array, simply chosing the first element of the array before clicking did the trick.

var result = await wbView1.EvaluateJavaScriptAsync("document.getElementsByClassName('load_more_btn ib')[0].click();");
Alejandro
  • 308
  • 2
  • 11