Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

javascript - Javacsript + Android Webview:以编程方式单击按钮(Javacsript + Android Webview: click button programmatically)

I am writing an Android App.(我正在编写一个Android应用程序。)

The App opens a website in Webview.(该应用程序在Webview中打开一个网站。) The website shows a rating bar, ie a rating bar that consists of 5 stars (ie buttons) to click on, but I want to click the stars programmatically from the app.(该网站显示了一个等级栏,即由5个星(即按钮)组成的等级栏,可以单击,但我想以编程方式从应用程序中单击星。) From the developer console in Chrome I got the javascript code of that website.(从Chrome开发者控制台中,我获得了该网站的javascript代码。) This is the javascript code for the first star of the ratinbgbar:(这是ratinbgbar的第一颗星的javascript代码:) <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?(我怎样才能做到这一点?)   ask by user12432260 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 } }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...