onWebView检查网页中文
问题:要检查网页中的一段文本:
开始我是这样写的:
private final static String SPECIFIED_TEXT = "这个是一段中文"; onWebView().check(webContent(containingTextInNode(SPECIFIED_TEXT )));
然后直接报错了 从adb logcat看到的结果是网页中文显示为乱码,尝试输出了一下每个中文的长度都是3;但是可以看到网页结构和数据 可以看到数据文本数据是在<p></p> <h2></h2> 里面
不死心啊: 把检查代码全部从库里面拷贝出来 改成
onWebView().check(userWebContent(containingTextInNode(SPECIFIED_TEXT , "p")));
/**
* 为了把网页输出出来
* @param xml
*/
public static void logall(String xml) {
if (xml.length() > 4000) {
for (int i = 0; i < xml.length(); i += 4000) {
if (i + 4000 < xml.length())
Log.i(TAG, xml.substring(i, i + 4000));
else
Log.i(TAG, xml.substring(i, xml.length()));
}
} else
Log.i(TAG, xml);
}
/**
* A WebAssertion which asserts that the document is matched by th provided matcher.
*/
public static WebAssertion<Document> userWebContent(final Matcher<Document> domMatcher) {
checkNotNull(domMatcher);
return webMatches(transform(script("return document.documentElement.outerHTML;"),
new TransformingAtom.Transformer<Evaluation, Document>() {
@Override
public Document apply(Evaluation eval) {
if (eval.getValue() instanceof String) {
try {
// Logall( "eval.getValue() " + (String)eval.getValue()); //这个地方能完整输出网页数据-不乱码的
// return TagSoupDocumentParser.newInstance().parse((String) eval.getValue()); //这个方法不能显示中文
org.jsoup.helper.W3CDom w3cDom = new W3CDom();
org.jsoup.nodes.Document doc = Jsoup.parseBodyFragment((String) eval.getValue()); //org.jsoup.nodes.Document无法转换为org.w3c.dom.Document
return w3cDom.fromJsoup(doc);
} catch (Exception se) {
throw new RuntimeException("Parse failed: " + eval.getValue(), se);
}
}
throw new RuntimeException("Value should have been a string: " + eval);
}
}), domMatcher,
new WebViewAssertions.ResultDescriber<Document>() {
@Override
public String apply(Document document) {
try {
DOMSource docSource = new DOMSource(document);
Transformer tf = TransformerFactory.newInstance().newTransformer();
StringWriter writer = new StringWriter();
StreamResult streamer = new StreamResult(writer);
tf.transform(docSource, streamer);
return writer.toString();
} catch (TransformerException e) {
return "Could not transform!!!" + e;
}
}
});
}
/**
* Returns a matcher that matches Documents that have a body containing the given test.
*/
public static Matcher<Document> containingTextInNode(String text, final String nodeNme) {
checkNotNull(text);
return withNodeName(withTextContent(containsString(text)), nodeNme);
} /**
* Returns a matcher that matches {@link Document}s with body that matches the given matcher.
*/
public static Matcher<Document> withNodeName(final Matcher<Element> bodyMatcher, final String nodeNme) {
checkNotNull(bodyMatcher);
return new TypeSafeMatcher<Document>() {
@Override
public void describeTo(Description description) {
description.appendText("with NodeName: ");
bodyMatcher.describeTo(description);
} @Override
public boolean matchesSafely(Document document) {
NodeList nodeList = document.getElementsByTagName(nodeNme);
if (nodeList.getLength() == 0) {
return false;
}
// showNode(nodeList, "");
for (int i = 0; i < nodeList.getLength(); i++) {
if (bodyMatcher.matches(nodeList.item(i))) {
return true;
}
}
return false;
}
};
} /**
* 将节点集放入已排序的集合中时,W3C 将其称为 NodeList;可以按从零开始的索引检索数据。
*
* @param nodeList
* @param path
*/
public static void showNode(NodeList nodeList, String path) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node mobilePhone = nodeList.item(i);
int destination = mobilePhone.getTextContent().length();
NodeList mobileNodeList = mobilePhone.getChildNodes();
if (mobileNodeList.getLength() > 0) {
showNode(mobileNodeList, path + "-" + mobilePhone.getNodeName());
} else {
Log.i(TAG, path + "-" + mobilePhone.getNodeName() + ":" + destination + " " + mobilePhone.getTextContent()); //无子节点了就显示
}
}
}
//上面我们用了jsoup库,gradle里面增加库依赖
//还要注意Document转换
dependencies {
compile 'org.jsoup:jsoup:1.9.2'
androidTestCompile 'org.jsoup:jsoup:1.9.2' //测试用这个
}
至此可以顺利检查到网页中的中文啦,代码比较乱,将就着先用吧
onWebView检查网页中文的更多相关文章
- 解决Ubuntu下Chrome浏览器网页中文字体混乱
在Ubuntu下使用Chrome浏览器时碰到了网页中文字体混乱的现象: 黑体和楷体混杂,看起来非常不美观. 这是由于许多网页并没有指定字体,然后浏览器将调用系统默认字体配置. 首先,安装文泉驿字体: ...
- 【转载】 IE/Firefox每次刷新时自动检查网页更新,无需手动清空缓存的设置方法
[参考了别人的文章]我们做技术,经常在写页面的时候需要多次刷新测试,可是浏览器都有自己的 缓存机制,一般CSS和图片都会被缓存在本地,这样我们修改的CSS就看不到效果 了,每次都去清空缓存,再刷新看效 ...
- IE/Firefox每次刷新时自动检查网页更新,无需手动清空缓存的设置方法
浏览器都有自己的 缓存机制,一般CSS和图片都会被缓存在本地,这样我们修改的CSS就看不到效果 了,每次都去清空缓存,再刷新看效果,这样操作太麻烦了.在IE下我们可以直接 去修改internet选项/ ...
- 使用notepad++学习python爬虫,print网页中文乱码问题
今天学习使用python爬虫的时候发现爬到的网页中文会乱码,一直网上搜索解决办法,一个一个试验过去,发现还是乱码,然后我就开始使用其它方法测试,用python自带的编辑器打开是正常的,发现是notep ...
- 爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,loads,dump,load方法介绍
爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,load ...
- [Python] - 使用chardet检查网页编码格式时发现的问题
最近在使用chardet检查网页编码格式时发现如下问题: 用urllib打开网页再检查编码格式和用urllib2打开网页检查编码格式结果不一样,所以urllib2打开可能导致问题,需要关注. 查看了相 ...
- node爬虫之gbk网页中文乱码解决方案
之前在用 node 做爬虫时碰到的中文乱码问题一直没有解决,今天整理下备忘.(PS:网上一些解决方案都已经不行了) 中文乱码具体是指用 node 请求 gbk 编码的网页,无法正确获取网页中的中文(需 ...
- mac下网页中文字体优化
最近某人吐槽某门户网站在mac下chrome字体超丑,然后发现虽然现在mac用户越来越多,但是大家依然无视mac下的字体差异,于是研究了下mac下网页中的中文字体,和大家分享. 看了一遍国内各大门户和 ...
- Font-Spider 一个神奇的网页中文字体工具,就是这么任性
文章摘要: 1>> font-spider 字体神奇 由于活动项目推广的需要,页面需要用到一些漂亮好看的字体,example : 邯郸-韩鹏毛遂体.ttf. 方正喵呜.ttf 我看 ...
随机推荐
- SpringBoot Rabbitmq接收消息
官网地址:https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-amqp ...
- .Net MVC断点进不去
.Net MVC断点进不去 1.httpget httppost 2.启动项设为UI 3.基于页面没错误的情况下
- PHP面向对象关键词static 、self
知识点: 一.static可以修饰类内的属性或方法,被修饰的属性或方法在类外部,不能被实例化成对象访问,而是使用类本身进行访问,而静态的方法如果想使用静态的属性,则需要用self::这样的写法来访问静 ...
- Struts1 多个配置文件的实现
在Struts 1.0中,我们只能在web.xml中为ActionServlet指定一个配置文件,这对于我们这些网上的教学例子来说当然没什么问题,但是在实际的应用开发过程中,可能会有些麻烦.因为许多开 ...
- 不允许有匹配 "[xX][mM][lL]" 的处理指令目标。
xml文件报错: 不允许有匹配 "[xX][mM][lL]" 的处理指令目标. 指的注意的是规范的XML格式: <?xml version="1.0" ...
- [2018-9-4T2]探索黑暗dark
题目大意:有一棵树,第$i$个点的点权为$s_i(s_1>0)$.初始有每个点都是亮的.$m$次修改,每次改变一个点的亮暗,回答包含$1$的全亮的连通块中点权和最大的连通块的和的值. 题解:正解 ...
- 使用C#创建windows服务程序
创建windows服务项目 一.创建服务 1.文件->新建->项目->windows桌面->windows服务,修改你要的项目名称.我这不改名,仍叫WindowsService ...
- 导入android源码中的APP源码到eclipse
导入android源码中的APP源码到eclipse 一般最简单的办法就是创建新的android工程,选择create project from existing source选项,直接导入源码就OK ...
- 飞镖(bzoj 2335)
Description 飞镖是在欧洲颇为流行的一项运动.它的镖盘上分为20个扇形区域,分别标有1到20的分值,每个区域中有单倍.双倍和三倍的区域,打中对应的区域会得到分值乘以倍数所对应的分数.例如打中 ...
- UVa10891 Game of Sum
给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能从一端选取.两人都采用最优策略,A先手,问A和B各自得到数字的和的差值最大为多少? 区间DP F[i][j]表示区间i~j内A能得到的最大数 ...