2014-02-26 115 views
-1

我想用HtmlUnit登錄約會網站http://www.pof.com,並瀏覽其網頁。我無法成功完成第一步(登錄)。我是初學者,所以請查看下面的代碼。難道是網站限制了機器人本身?試圖使用HtmlUnit瀏覽網站

import com.gargoylesoftware.htmlunit.WebClient; 
import com.gargoylesoftware.htmlunit.html.HtmlPage; 
import com.gargoylesoftware.htmlunit.html.HtmlForm; 
import com.gargoylesoftware.htmlunit.html.HtmlTextInput; 
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; 

public class DataWebBot { 


public static void main(String[] args) throws IOException { 

    // navigate & fetch HTML   
    final WebClient webClient = new WebClient(); 

    //Get first page 
    final HtmlPage pofLoginPage = webClient.getPage("http://www.pof.com"); 

    //Get the login form and find the user/password fields and submit button. 
    final HtmlForm loginForm = pofLoginPage.getFormByName("frmLogin"); 
    final HtmlTextInput usernameField = loginForm.getInputByName("username"); 
    final HtmlTextInput passwordField = loginForm.getInputByName("password"); 
    final HtmlSubmitInput loginButton = loginForm.getInputByName("submitbutton"); 

    //Set value of text field, login and get the second page 
    usernameField.setValueAttribute("myUsername"); 
    passwordField.setValueAttribute("myPassword"); 

    webClient.getCookieManager().getCookies(); 

    System.out.println("Complete"); 
    } 
} 

而且我得到這些錯誤:

Feb 26, 2014 4:59:38 PM com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject jsConstructor 
WARNING: Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash.7'. 
Feb 26, 2014 4:59:38 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 
SEVERE: runtimeError: message=[Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash.7'.] sourceName=[http://www.pof.com/javascript/versioned/pofcommon.min.1386808612.js] line=[5] lineSource=[null] lineOffset=[0] 
Feb 26, 2014 4:59:38 PM com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject jsConstructor 
WARNING: Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash.6'. 
Feb 26, 2014 4:59:38 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 
SEVERE: runtimeError: message=[Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash.6'.] sourceName=[http://www.pof.com/javascript/versioned/pofcommon.min.1386808612.js] line=[5] lineSource=[null] lineOffset=[0] 
Feb 26, 2014 4:59:38 PM com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject jsConstructor 
WARNING: Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash'. 
Feb 26, 2014 4:59:38 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 
SEVERE: runtimeError: message=[Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash'.] sourceName=[http://www.pof.com/javascript/versioned/pofcommon.min.1386808612.js] line=[5] lineSource=[null] lineOffset=[0] 
Feb 26, 2014 4:59:38 PM com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject jsConstructor 
WARNING: Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash'. 
Feb 26, 2014 4:59:38 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 
SEVERE: runtimeError: message=[Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash'.] sourceName=[http://www.pof.com/javascript/versioned/pofcommon.min.1386808612.js] line=[5] lineSource=[null] lineOffset=[0] 
Feb 26, 2014 4:59:39 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 
WARNING: Obsolete content type encountered: 'text/javascript'. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:769] Error in expression. (Invalid token "}". Was expecting one of: <S>, <NUMBER>, "inherit", <IDENT>, <STRING>, <PLUS>, <HASH>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>, "-".) 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:8694] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".) 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:8694] Ignoring the following declarations in this rule. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:8899] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".) 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:8899] Ignoring the following declarations in this rule. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:9105] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".) 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:9105] Ignoring the following declarations in this rule. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:9310] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".) 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:9310] Ignoring the following declarations in this rule. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:9544] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".) 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:9544] Ignoring the following declarations in this rule. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:12238] Error in style rule. (Invalid token ":". Was expecting one of: <EOF>, <S>, <NUMBER>, "inherit", <IDENT>, <STRING>, <PLUS>, <COMMA>, <HASH>, <IMPORTANT_SYM>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>, "}", ";", "/", "-".) 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:12238] Ignoring the following declarations in this rule. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:16850] Error in style rule. (Invalid token "*". Was expecting one of: <EOF>, <S>, <IDENT>, "}", ";".) 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:16850] Ignoring the following declarations in this rule. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error 
WARNING: CSS error: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:19023] Error in style rule. (Invalid token ":". Was expecting one of: <EOF>, <S>, <NUMBER>, "inherit", <IDENT>, <STRING>, <PLUS>, <COMMA>, <HASH>, <IMPORTANT_SYM>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>, "}", ";", "/", "-".) 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning 
WARNING: CSS warning: 'http://www.pof.com/css/versioned/main.min.1392865551.css' [1:19023] Ignoring the following declarations in this rule. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject jsConstructor 
WARNING: Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash.7'. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 
SEVERE: runtimeError: message=[Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash.7'.] sourceName=[http://www.google-analytics.com/ga.js] line=[24] lineSource=[null] lineOffset=[0] 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject jsConstructor 
WARNING: Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash.6'. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 
SEVERE: runtimeError: message=[Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash.6'.] sourceName=[http://www.google-analytics.com/ga.js] line=[24] lineSource=[null] lineOffset=[0] 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject jsConstructor 
WARNING: Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash'. 
Feb 26, 2014 4:59:40 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 
SEVERE: runtimeError: message=[Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash'.] sourceName=[http://www.google-analytics.com/ga.js] line=[24] lineSource=[null] lineOffset=[0] 
Feb 26, 2014 4:59:41 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 
WARNING: Obsolete content type encountered: 'text/javascript'. 
Exception in thread "main" java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.HtmlPasswordInput cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlTextInput 
    at DataWebBot.main(DataWebBot.java:28) 

問題是什麼?我錯誤地使用了餅乾嗎?我需要他們繼續瀏覽網站的其他年齡段,不是嗎?請幫忙。謝謝!

回答

0

的錯誤似乎是相當清楚的:

Exception in thread "main" java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.HtmlPasswordInput cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlTextInput 

所以改變這樣的:

final HtmlTextInput passwordField = loginForm.getInputByName("password"); 

進入這個:

final HtmlPasswordInput passwordField = loginForm.getInputByName("password"); 

,將讓你通過e xception。作爲一個附註,你可能會遇到JavaScript問題,但這是另一回事。

+0

謝謝。這是有幫助的 - 不知道爲什麼我錯過了它。 –