iOS之WKWebView修改网页页面的值
来源:互联网 发布:朴树旅途知乎 编辑:程序博客网 时间:2024/05/22 00:25
使用WKWebview时导入
#import <webkit/WebKit.h>
使用webView时,加载js时,js代码中不能有变量表示,
wkwebView,加载js 时,代码中可以用变量;
参考:http://blog.csdn.net/u011154007/article/details/52944767
在网页加载完成的时候====
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
//获取整个网页的HTML代码
NSString *doc =@"document.body.outerHTML";
[webView evaluateJavaScript:doc
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"html--:%@",htmlStr);
}] ;
//获取当前页面的url
NSString *doc01 =@"document.location.href";
[webView evaluateJavaScript:doc01
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"url--:%@",htmlStr);
}] ;
//获取页面的title
NSString *doc02 =@"document.title";
[webView evaluateJavaScript:doc02
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"url--:%@",htmlStr);
}] ;
//修改界面的元素的值
NSString *doc03 =@"document.getElementsByName('q')[0].value='朱祁林';";
[webView evaluateJavaScript:doc03
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"修改元素--:%@",htmlStr);
}] ;
//提交表单
NSString *doc04 =@"document.forms[0].submit();";
[webView evaluateJavaScript:doc04
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"提交表单--:%@",htmlStr);
}] ;
//获取所有的HTML
NSString *doc05 =@"document.documentElement.innerHTML";
[webView evaluateJavaScript:doc05
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"获取所有的HTML--:%@",htmlStr);
}] ;
//获取网页的一个值
NSString *doc06 =@"document.getElementById('title').innerText";
[webView evaluateJavaScript:doc06
completionHandler:^(id_Nullable htmlStr,NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"获取网页的一个值--:%@",htmlStr);
}] ;
//获取网页内容的高度
[webView evaluateJavaScript:@"document.body.offsetHeight;"
completionHandler:^(id _Nullable any, NSError * _Nullable error) {
NSString *heightStr = [NSString stringWithFormat:@"%@",any];
}];
NSLog(@"完成加载");
}
if ([title compare: @"Google"]==NSOrderedSame ) {
[webView evaluateJavaScript:@"var script = document.createElement_x('script');"
"script.type = 'text/javascript';"
"script.text = "function myFunction() {"
"var field = document.getElementsByName('q')[0];"
"field.value='朱祁林';"
"document.forms[0].submit();"
"}";"
"document.getElementsByTagName_r('head')[0].appendChild(script);"
completionHandler:^(id _Nullable htmlStr, NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"获取网页的一个值--:%@",htmlStr);
}] ;
];
[webView evaluateJavaScript:@"myFunction();"
completionHandler:^(id _Nullable htmlStr, NSError * _Nullable error) {
if (error) {
NSLog(@"JSError:%@",error);
}
NSLog(@"获取网页的一个值--:%@",htmlStr);
}] ;
];}
看上面的代码:
a、首先通过js创建一个script的标签,type为'text/javascript'。
b、然后在这个标签中插入一段字符串,这段字符串就是一个函数:myFunction,这个函数实现google自动搜索关键字的功能。
c、然后使用webView evaluateJavaScript执行myFunction函数。
[webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"var edit_card_num = setInterval(function () {if (document.getElementById('cardNumber')) {cardNumber = document.getElementById('cardNumber');cardNumber.value = '6216261000000000018';cardNumber.setAttribute('disabled', 'disabled');if (document.getElementsByClassName('up-btn next cardbin')[0]) {document.getElementsByClassName('up-btn next cardbin')[0].click();clearInterval(edit_card_num)}}}, 5)\";"
"document.body.appendChild(script);"];
NSString *jsStr=[NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"%@\";"
"document.body.appendChild(script);",self.jsStr];
[webView stringByEvaluatingJavaScriptFromString:jsStr];
=======webview 修改网页====
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *currentURL = [webViewstringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSLog(@"--wangzhi--%@",currentURL);
NSString *allHtml =@"document.getElementsByTagName('html')[0].innerHTML";
NSString *allhtml=[webViewstringByEvaluatingJavaScriptFromString:allHtml];
NSLog(@"所有的html%@",allhtml);
NSString *fieldStr=[webViewstringByEvaluatingJavaScriptFromString:@"document.getElementsById('session_email_or_mobile_number').value
'sssss'
;"];NSLog(@"strv输入框%@",fieldStr);
NSString *title = [webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"标题--%@",title);
[webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement_x('script');"
"script.type = 'text/javascript';"
"script.text = \"function myFunction() {\"var field = document.getElementsById('session_email_or_mobile_number');"
"field.value='朱祁林';"
"document.forms[0].submit();"
"}\";"
"document.getElementsByTagName_r('head')[0].appendChild(script);"];
[webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
}
- iOS之WKWebView修改网页页面的值
- iOS开发中OC与H5网页交互之OC传值给JS(WKWebView)
- iOS OC与H5网页交互之OC传值给JS(WKWebView)
- ios之wkwebview与UIwebview的对比
- iOS实现微信网页进度条,WKWebview进度条的实现
- iOS WKWebView加载网页禁止缩放的问题
- iOS WKWebView 加载网页 点击link不会跳转的解决方案
- IOS 进阶之 WKWebView
- iOS开发 之 WKWebView
- iOS WKWebView高度自适应以及截获页面点击的url
- iOS WKWebView高度自适应以及截获页面点击的url
- iOS-获取UIWebView或者WKWebView页面的视频连接
- iOS WKwebview自动实现缩放网页适应屏幕和侧滑返回上个页面
- iOS WKWebView的使用
- iOS-WKWebView的封装
- iOS:与网页WebView的数据交互及网页内事件响应(WKWebView,bridge))
- iOS WKWebView添加网页加载进度条
- kidd风的IOS日志之从UIWebView到WKWebView
- STL学习感悟(一)
- RocketMQ Quick Start
- 不能错过的Sketch实用新技巧和资源集锦
- 求链表的中间节点
- AttributeError: module 'tensorflow' has no attribute 'global_variables_initializer'解决
- iOS之WKWebView修改网页页面的值
- 安卓原生APP内嵌webview调试
- 如何在extenreports添加错误的截图
- UE4版本控制
- Cocos2d-x 多分辨率适配完全解析 来源:CocoaChina
- 全国电赛K题江苏省二等奖----王澳刚
- 编程题——地下迷宫
- Samba 4.3.11无需密码访问配置。
- UVa133-The Dole Queue