0

I am writing an Android App. The App opens a website in Webview. The website shows a rating bar, i.e. a rating bar that consists of 5 stars (i.e. buttons) to click on, but I want to click the stars programmatically from the app.

From the developer console in Chrome I got the javascript code of that website. This is the javascript code for the first star of the ratinbgbar:

<div class="ratingbar-container" steps="5">
  <div class="ratingbar-wrap" style="height: 40px; font-size: 38px;">
    <div class="rating" rel="1">
       <i class="svi-star ratingicon-full"></i>
       <i class="svi-star-o ratingicon-outline">
       </i>
    </div>
    <div class="rating" rel="2">

Now I want my App to click the star programmatically. In the code below I tried to click the first star programmtaically like this but nothing happens:

wb.setWebViewClient(new WebViewClient() {          
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequestrequest) {
        view.loadUrl(request.getUrl().toString());                               
        String jsclick = "javascript:document.getElementsByClassName('rating')[0].click();";
        view.loadUrl(jsclick);

        return false;
    }           
});

How can I do this?

1 Answers1

-1

You need an event listener on the clicks

let ratings = document.querySelectorAll(".rating");
forof(const rating of ratings){
    rating.addEventListener('click',function(e){
      //Whatever code you want to run
    }
}
Trukken
  • 101
  • 10