开发者社区> 问答> 正文

Java模拟登录新浪报错?报错

import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;


public class SinaLoginTest {
 public static void main(String[] args) throws Exception {
 WebClient client = new WebClient(BrowserVersion.FIREFOX_24);
 client.getOptions().setJavaScriptEnabled(true); // 默认执行js,如果不执行js,则可能会登录失败,因为用户名密码框需要js来绘制。
 client.getOptions().setCssEnabled(false);
 client.setAjaxController(new NicelyResynchronizingAjaxController());
 client.getOptions().setThrowExceptionOnScriptError(false);


 HtmlPage page = client
 .getPage("http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.16)");
 // System.out.println(page.asText());


 // 登录


 HtmlInput ln = page.getHtmlElementById("username");
 HtmlInput pwd = page.getHtmlElementById("password");
 HtmlInput btn = page
 .getFirstByXPath(".//*[@id='vForm']/div[3]/ul/li[6]/div[2]/input");


 ln.setAttribute("value", "159890555555");
 pwd.setAttribute("value", "nmj121");


 HtmlPage page2 = btn.click();
 // 登录完成,现在可以爬取任意你想要的页面了。
 System.out.println("\n\n\n");
 // System.out.println(page2.asText());


 HtmlPage page3 = client.getPage("http://weibo.com/friends?leftnav=1&wvr=5&isfriends=1&step=2");
 System.out.println(" : " + page3.asXml());


 client.closeAllWindows();
 }


}


 

 
 
 
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	FIREFOX_24 cannot be resolved or is not a field
	The method getOptions() is undefined for the type WebClient
	The method getOptions() is undefined for the type WebClient
	NicelyResynchronizingAjaxController cannot be resolved to a type
	The method getOptions() is undefined for the type WebClient
	The method getPage(URL) in the type WebClient is not applicable for the arguments (String)
	Type mismatch: cannot convert from HtmlElement to HtmlInput
	Type mismatch: cannot convert from HtmlElement to HtmlInput
	The method getFirstByXPath(String) is undefined for the type HtmlPage
	The method setAttribute(String, String) is undefined for the type HtmlInput
	The method setAttribute(String, String) is undefined for the type HtmlInput
	Type mismatch: cannot convert from Page to HtmlPage
	The method getPage(URL) in the type WebClient is not applicable for the arguments (String)
	The method closeAllWindows() is undefined for the type WebClient

	at com.qjj.test.SinaLoginTest.main(SinaLoginTest.java:14) 



展开
收起
爱吃鱼的程序员 2020-06-09 14:06:35 554 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    感觉是运行期你使用的第三方库没有进入classpath

    2020-06-09 14:06:52
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载