Re: [問題]用 javascript 點擊 webview element

作者: purin88 (原來我是憤怒的鄉民)   2015-05-12 21:55:16
※ 引述《snoopy1530 (玲玲)》之銘言:
: 大家好
: 我現在用Robotium 這個 android test Framework 來測試 APK (沒有 source code)
: 在要模擬點擊 Web element 遇到一個麻煩,我想要使用 javascipt 來點擊web element
: 但不管怎樣試都無法work,程式碼如下
: List<WebElement> webelement = solo.getCurrentWebElements();
: for(WebElement we : webelement){
: webview.loadUrl("javascript:
: var ev=document.getElementById(we.getId());
: ev.initEvent('click',true,false);
: document.dispatchEvent(ev);");
: }
: 也試過
: webview.loadUrl("javascript:getElementById(we.getId()).click()")
: 我再想是不是 getElementById() 只能接受 固定值?
: 也試過用參數代入,也是無法work
: final web_id = we.getId()
: webview.loadUrl("javascript:getElementById(web_id).click()")
: 會想要用javascript去點擊web
: 是因為用過了Robotium 本身提供的 solo.clickOnWebElement
: 也是無法work
: 因為本身對於web 跟javascript不太熟
: 不知道有沒有人可指點一下
: 謝謝
1、
wb.loadUrl("javascript:<script> </script>");
use,
wb.loadDataWithBaseURL(null,"<script>
</script>","text/html","utf-8",null);
2、
contentWebView.setWebViewClient(new MyWebViewClient());
private void loadJs(){
contentWebView.loadUrl("javascript:(function(){" +
"var objs = document.getElementsByTagName(\"img\"); " +
"for(var i=0;i<objs.length;i++) " +
"{"
+ " objs[i].onclick=function() " +
" { "
+ " window.imagelistner.openImage(this.src); " +
" } " +
"}" +
"})()");
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
view.getSettings().setJavaScriptEnabled(true);
super.onPageFinished(view, url);
loadJs();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.getSettings().setJavaScriptEnabled(true);
super.onPageStarted(view, url, favicon);
}
@Override
public void onReceivedError(WebView view, int errorCode, String
description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
3、
String js = "javascript:(function () {
// Do some JS in here
})()";
// NOTE: webView is just a reference to a some global variable in your
Activity
webView.loadUrl(js);

Links booklink

Contact Us: admin [ a t ] ucptt.com