如何解决在Flutter WebView中以编程方式登录用户
我正在使用webview_Flutter。
我目前在我的应用程序中有一个本机登录页面,其中包含用户名和密码。我试图找到一种自动在webview的特定页面上填写用户名和密码并提交登录请求的方法。
尝试这样做
webViewController.evaluateJavascript(
'''
var email = document.getElementById("CustomerEmail");
var password = document.getElementById("CustomerPassword");
email.value = "user@gmail.com";
password.value = "test123";
'''
);
但出现以下错误:
Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=3,WKJavaScriptExceptionMessage=TypeError: null is not an object (evaluating 'email.value = "user@gmail.com"'),WKJavaScriptExceptionColumnNumber=11,WKJavaScriptExceptionSourceURL=undefined,NSLocalizedDescription=A JavaScript exception occurred})
非常感谢您提供的任何帮助。谢谢。
解决方法
代码有效,我把它放在错误的位置。
代码应放在onPageFinished下。我以前在onWebViewCreated下拥有它。
以下代码正常工作。
WebView(
initialUrl: widget.url,onPageFinished: (_) {
setState(() {
print("loggedin " + loggedIn.toString());
if(loggedIn == false) {
loggedIn = true;
_controller.future
.then((value) =>
value.evaluateJavascript('''
var email = document.getElementById("CustomerEmail");
var password = document.getElementById("CustomerPassword");
email.value = "user@gmail.com";
password.value = "test123";
document.getElementById('customer_login').submit();
'''));
}
});
},javascriptMode: JavascriptMode.unrestricted,onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。