Javacsript + Android Webview:以编程方式单击按钮 [英] Javacsript + Android Webview: click button programmatically
问题描述
我正在编写一个 Android 应用程序.该应用程序在 Webview 中打开一个网站.该网站显示了一个评级栏,即一个由 5 颗星(即按钮)组成的评级栏供点击,但我想从应用程序中以编程方式点击这些星星.
从 Chrome 中的开发者控制台我得到了那个网站的 javascript 代码.这是Ratinbgbar第一颗星的javascript代码:
<div class="rating" rel="2">
现在我希望我的应用程序以编程方式单击星星.在下面的代码中,我尝试像这样以编程方式单击第一颗星,但没有任何反应:
wb.setWebViewClient(new WebViewClient() {@覆盖public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequestrequest) {view.loadUrl(request.getUrl().toString());String jsclick = "javascript:document.getElementsByClassName('rating')[0].click();";view.loadUrl(jsclick);返回假;}});
我该怎么做?
你需要一个点击事件监听器
let ratings = document.querySelectorAll(".rating");forof(const rating of ratings){rating.addEventListener('点击',function(e){//任何你想运行的代码}}
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?
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
}
}
这篇关于Javacsript + Android Webview:以编程方式单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!