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 我看 ...
随机推荐
- node + express + iis + iisnode + urlrewrite搭建站点
前提条件:安装iis的电脑 准备条件: 1.下载iisnode 地址https://github.com/tjanczuk/iisnode/wiki/iisnode-releases 安装 2.下载 ...
- 【bzoj1047】[HAOI2007]理想的正方形 二维RMQ
题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入 第一行为3个整数,分别表示a,b,n的值第二行至第a+1行每行为b个非 ...
- hdu 2510 符号三角形 (DFS+打表)
符号三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 看了就学会之React redux入门示例
环境准备 为了方便,这里使用create-react-app搭建react环境 create-react-app mydemo 弹出配置 如果需要自定义react的配置,需要运行下面的命令把配置文件弹 ...
- [poj] 1375 Interval || 圆的切线&和直线的交点
原题 每组数据给出一些圆(障碍物)的圆心和半径,一个点和一条线段,求站在这个点,能开到的线段的部分的左端点和右端点.没有则输出"No View" 相当于求过该点的圆的两条切线,切线 ...
- [hdu] 5696 区间的价值 || 序列分治
原题 我们定义"区间的价值"为一段区间的最大值*最小值. 一个区间左端点在L,右端点在R,那么该区间的长度为(R−L+1). 求长度分别为1-n的区间的最大价值. 保证数据随机 因 ...
- HDU 4746 HDOJ Mophues 2013杭州网赛I题
比赛的时候就预感到这题能出,但是会耗时比较多.结果最后是出了,但是有更简单的题没出. 是不是错误的决策呢?谁知道呢 题目意思: 定义f(x) = x分解质因数出来的因子个数 如 x = p0 * p0 ...
- 雅礼集训 Day5 T3 题 解题报告
题 题目背景 由于出题人赶时间所以没办法编故事来作为背景. 题目描述 一开始有\(n\)个苹果,\(m\)个人依次来吃苹果,第\(i\)个人会尝试吃\(u_i\)或\(v_i\)号苹果,具体来说分三种 ...
- 静态区间第k大 树套树解法
然而过不去你谷的模板 思路: 值域线段树\([l,r]\)代表一棵值域在\([l,r]\)范围内的点构成的一颗平衡树 平衡树的\(BST\)权值为点在序列中的位置 查询区间第\(k\)大值时 左区间在 ...
- ubuntu启动报错 Errors were found while checking the disk-drive for /
开机报这个错误,主要原因是硬盘检测不通过导致的,下面介绍两种方法规避该问题: 修改grub 这个方法网上比较多,直接贴过来: 进入Ubuntu启动菜单时,光标选中 *Ubuntu 后,按键盘上的 e ...