我正在制作一个小应用程序来访问网站
public class Main_01 {
private static Logger log = Logger.getLogger(Main_01.class);
public static void main(String[] args) throws IOException {
BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.DEBUG);
Main_01 main = new Main_01();
try (final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER)) {
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.setCssErrorHandler(new SilentCssErrorHandler());
webClient.getOptions().setRedirectEnabled(false);
final HtmlPage pageHome = webClient.getPage("https://www.phpbb.com");
final HtmlPage page = webClient.getPage("https://www.phpbb.com/community/ucp.php?mode=login");
System.out.println(page.getTitleText());
final HtmlForm form = page.getFirstByXPath("//form[@action='./ucp.php?mode=login']");
System.out.println(form.asText());
}
}
}
gtFirstByXPath返回NullPointerException,但我确信"表单操作"存在,正如您所看到的
htmlUnit语法正确吗?感谢
表单像这样锁定
<form action="./ucp.php?mode=login&sid=966c26f6333def472ee4061a8cabfa96" method="post" id="login" data-focus="username">
属性值大于搜索字符串。
试试这个
page.getFirstByXPath("//form[starts-with(@action, './ucp.php?mode=login')]");
我不知道它为什么抛出异常。但这必须是工作://*[@id='wrapcentre']/form