我无法在HtmlUnit 2.19中使用Google Charts页面进行渲染。
我有以下java
代码:
public static void main(String arg[]) throws IOException{
String url = "http://localhost/chartsTest/test.html";
WebClient wc = new WebClient(BrowserVersion.CHROME);
HtmlPage p = null;
try {
System.out.println("Attempting to load page: " + url);
p = wc.getPage(url);
System.out.println("Sucsess!");
} catch (Exception e) {
System.err.println("Failed to get page");
}
HtmlDivision div = p.getHtmlElementById("img");
String content = div.asXml();
System.out.println(content);
div = p.getHtmlElementById("data");
content = div.asText();
System.out.println(content);
wc.close();
}
和我的test.html
页面是:
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", '1', {packages:['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2013', 1000, 400],
['2014', 1170, 460],
['2015', 660, 1120],
['2016', 1030, 540]
]);
var chart_div = document.getElementById('chart');
var chart_img = document.getElementById('img');
var chart_data = document.getElementById('data');
var chart = new google.visualization.AreaChart(chart_div);
google.visualization.events.addListener(chart, 'ready', function () {
var imgString = chart.getImageURI();
chart_img.innerHTML = '<img src="' + imgString + '">';
chart_data.innerHTML = imgString;
});
chart.draw(data);
}
</script>
</head>
<body>
<div id="chart"></div>
<div id="img"></div>
<div id="data"></div>
</body>
</html>
当我在网络浏览器中打开此页面时,我会获得svg图表,图表的图像,然后是图像的png 64位字符串。
然而,当我运行java应用程序时,我得到的输出是:
Attempting to load page: http://localhost/chartsTest/test.html
Oct 19, 2015 11:41:37 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Oct 19, 2015 11:41:37 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
Oct 19, 2015 11:41:37 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
Oct 19, 2015 11:41:42 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: 'https://ajax.googleapis.com/ajax/static/modules/gviz/1.0/core/tooltip.css' [3:38] Error in pseudo class or element. (Invalid token " ". Was expecting one of: <IDENT>, ":", <FUNCTION_NOT>, <FUNCTION_LANG>, <FUNCTION>.)
Oct 19, 2015 11:41:42 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning
WARNING: CSS warning: 'https://ajax.googleapis.com/ajax/static/modules/gviz/1.0/core/tooltip.css' [3:38] Ignoring the whole rule.
Sucsess!
<div id="img">
<img src="data:image/png;base64,"/>
</div>
data:image/png;base64,
图像似乎无法正确加载。
如果我在更改div的函数中放入一个日志,它只会在运行java应用程序时记录data:image/png;base64,
字符串。