admin管理员组

文章数量:1394076

I am trying to simulate the login with htmlunit. Although I wrote my code according to the examples, I have encountered a boring problem. Below are some message I have picked up from the console.

runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x'         error: Invalid selector: *:x).] sourceName=[.10.2.js] line=[1640] lineSource=[null] lineOffset=[0]

WARNING: Obsolete content type encountered: 'application/x-javascript'.

CSS error: '.css' [1:1] Error in style sheet. (Invalid token "\u9518". Was expecting one of: <EOF>, <S>, <IDENT>, "<!--", "-->", <HASH>, <IMPORT_SYM>, <PAGE_SYM>, <MEDIA_SYM>, <FONT_FACE_SYM>, <CHARSET_SYM>, ".", ":", "*", "[", <ATKEYWORD>.)

Aug 11, 2014 1:43:27 下午 .gargoylesoftware.htmlunit.DefaultCssErrorHandler error

CSS error: '.css' [1:1] Error in style sheet. (Invalid token "\u9518". Was expecting one of: <EOF>, <S>, <IDENT>, "<!--", "-->", <HASH>, <IMPORT_SYM>, <PAGE_SYM>, <MEDIA_SYM>, <FONT_FACE_SYM>, <CHARSET_SYM>, ".", ":", "*", "[", <ATKEYWORD>.)

runtimeError: message=[An invalid or illegal selector was specified (selector: ':input' error: Invalid selector: *:input).] sourceName=[.10.2.js] line=[1275] lineSource=[null] lineOffset=[0]

Here is my code. And I got nothing useful. I don't know why that happened and wish for any help.

    WebClient webClient = new WebClient(BrowserVersion.CHROME);     
    webClient.getOptions().setCssEnabled(true);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setTimeout(10000);

    DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider)webClient.getCredentialsProvider(); 
    credentialsProvider.addCredentials("username", "password"); 


    HtmlPage page = webClient.getPage(url); 
    HtmlForm loginForm = page.getFormByName("regform");         


    HtmlInput username = loginForm.getInputByName("txt_userid"); 
    HtmlInput password = loginForm.getInputByName("txt_userpw"); 

    username.setValueAttribute("username"); 
    password.setValueAttribute("password");         


    HtmlSubmitInput submit = page.getHtmlElementById("login_btn");

    webClient.waitForBackgroundJavaScript(30000);
    HtmlPage result = submit.click();


    FileWriter writer = new FileWriter("mofang.html"); 
    writer.write(result.getWebResponse().getContentAsString()); 
    writer.close(); 

I am trying to simulate the login with htmlunit. Although I wrote my code according to the examples, I have encountered a boring problem. Below are some message I have picked up from the console.

runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x'         error: Invalid selector: *:x).] sourceName=[http://user.mofangge./Scripts/inc/jquery-1.10.2.js] line=[1640] lineSource=[null] lineOffset=[0]

WARNING: Obsolete content type encountered: 'application/x-javascript'.

CSS error: 'http://user.mofangge./Content/Css/Style1/Main.css' [1:1] Error in style sheet. (Invalid token "\u9518". Was expecting one of: <EOF>, <S>, <IDENT>, "<!--", "-->", <HASH>, <IMPORT_SYM>, <PAGE_SYM>, <MEDIA_SYM>, <FONT_FACE_SYM>, <CHARSET_SYM>, ".", ":", "*", "[", <ATKEYWORD>.)

Aug 11, 2014 1:43:27 下午 .gargoylesoftware.htmlunit.DefaultCssErrorHandler error

CSS error: 'http://user.mofangge./Content/Css/Style1/Login.css' [1:1] Error in style sheet. (Invalid token "\u9518". Was expecting one of: <EOF>, <S>, <IDENT>, "<!--", "-->", <HASH>, <IMPORT_SYM>, <PAGE_SYM>, <MEDIA_SYM>, <FONT_FACE_SYM>, <CHARSET_SYM>, ".", ":", "*", "[", <ATKEYWORD>.)

runtimeError: message=[An invalid or illegal selector was specified (selector: ':input' error: Invalid selector: *:input).] sourceName=[http://user.mofangge./Scripts/inc/jquery-1.10.2.js] line=[1275] lineSource=[null] lineOffset=[0]

Here is my code. And I got nothing useful. I don't know why that happened and wish for any help.

    WebClient webClient = new WebClient(BrowserVersion.CHROME);     
    webClient.getOptions().setCssEnabled(true);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setTimeout(10000);

    DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider)webClient.getCredentialsProvider(); 
    credentialsProvider.addCredentials("username", "password"); 


    HtmlPage page = webClient.getPage(url); 
    HtmlForm loginForm = page.getFormByName("regform");         


    HtmlInput username = loginForm.getInputByName("txt_userid"); 
    HtmlInput password = loginForm.getInputByName("txt_userpw"); 

    username.setValueAttribute("username"); 
    password.setValueAttribute("password");         


    HtmlSubmitInput submit = page.getHtmlElementById("login_btn");

    webClient.waitForBackgroundJavaScript(30000);
    HtmlPage result = submit.click();


    FileWriter writer = new FileWriter("mofang.html"); 
    writer.write(result.getWebResponse().getContentAsString()); 
    writer.close(); 
Share Improve this question asked Aug 11, 2014 at 5:53 user3761116user3761116 1521 silver badge4 bronze badges 1
  • 1 How important is CSS for you? Most probably you'll be able to disable CSS and go ahead. – Paddy Commented Aug 11, 2014 at 16:26
Add a ment  | 

1 Answer 1

Reset to default 5

Put

webClient.waitForBackgroundJavaScript(30000);

after

HtmlPage result = submit.click()

本文标签: javahtmlunitAn invalid or illegal selector was specifiedStack Overflow