问题
final WebClient webClient = new WebClient();
webClient.setJavaScriptEngine(new JavaScriptEngine(webClient));
HtmlPage page1 = null;
try {
page1 = webClient.getPage("http://www.facebook.com");
} catch (IOException e) {
e.printStackTrace();
}
final HtmlForm form = (HtmlForm) page1.getElementById("login_form");
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Log In").get(0);
final HtmlTextInput textField = (HtmlTextInput) page1.getElementById("email");
textField.setValueAttribute("test@test.com");
final HtmlPasswordInput textField2 = (HtmlPasswordInput) page1.getElementById("pass");
textField2.setValueAttribute("password1");
try {
HtmlPage page2 = button.click();
System.out.println(page2.getTitleText());
} catch (IOException e) {
e.printStackTrace();
}
I'm trying to use the HTMLUnit to log into an account on facebook. However I'm getting this error:
I'm looking to create a program that automates some of the things that I do with my pages that I have on facebook. I know something like this probably already exists but I'm making it for fun.
======= EXCEPTION START ========
EcmaError: lineNumber=[97] column=[0] lineSource=[<no source>] name=[TypeError] sourceName=[https://fbstatic-a.akamaihd.net/rsrc.php/v2/y8/r/ElsISGBmlSN.js] message=[TypeError: Cannot find function addImport in object [object CSSStyleSheet]. (https://fbstatic-a.akamaihd.net/rsrc.php/v2/y8/r/ElsISGBmlSN.js#97)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function addImport in object [object CSSStyleSheet]. (https://fbstatic-a.akamaihd.net/rsrc.php/v2/y8/r/ElsISGBmlSN.js#97)
回答1:
I stopped getting this error when I added a BrowserVersion into the WebClient constructor:
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38);
来源:https://stackoverflow.com/questions/32192272/htmlunit-facebook-login