gpt4 book ai didi

javascript - Android WebView 编译表单并使用 Javascript 提交

转载 作者:太空狗 更新时间:2023-10-29 14:09:50 28 4
gpt4 key购买 nike

我正在尝试填写此表格:

http://www.lbalberti.it/whatsup.asp?codist=57247

我能够向两个文本框插入值,但按钮不起作用。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
final String user = getIntent().getStringExtra("username");
final String psw = getIntent().getStringExtra("password");

MyWebView view = new MyWebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.loadUrl("http://www.lbalberti.it/whatsup.asp?codist=57247");
view.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView v, String url) {
v.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView v, String url) {
v.loadUrl("javascript:" +
"var y = document.getElementsByName('login')[0].value='"+user+"';" +
"var x = document.getElementsByName('password')[0].value='"+psw+"';");
}
});
setContentView(view);
}

class MyWebView extends WebView {
Context context;
public MyWebView(Context context) {
super(context);
this.context = context;
}
}

以下代码无效:
"var k = document.getElementByTagName('form')[0].submit();"
或者
“var k = document.getElementByName('newlogin')[0].submit();”

最佳答案

 @Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
view.evaluateJavascript("javascript:document.getElementById('username').value ='" + strUsername + "';javascript:document.getElementById('password').value = '" + strPassword + "';javascript:document.getElementById('loginButton').click();", null);
} else {
view.loadUrl("javascript:document.getElementById('username').value = '" + strUsername + "';javascript:document.getElementById('password').value = '" + strPassword + "';javascript:document.getElementById('loginButton').click();");
}

}

关于javascript - Android WebView 编译表单并使用 Javascript 提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905821/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com