简体   繁体   中英

Java HtmlUnit click on anchor

I am trying to make java app using htmlunit to get info from http://www.jobolizer.com site. So the thing is I have to fill the textbox with my url and click on anchor to submit form. The first part works great (finding form and filling form textbox with my data), but I can't find the anchor using getByXPath() method, the anchor does not have name or value. 在此输入图像描述

Here is my code:

public class JobolizerCrawler {
    private final String jobolizerUrl = "http://www.jobolizer.com";
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);

    public JobolizerCrawler () {
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
    }
    public void fillTextBoxWithUrl(String vacancyURL) throws IOException {

        final HtmlPage page = webClient.getPage(jobolizerUrl);
        System.out.println(page.asText());
        final HtmlForm form = page.getFirstByXPath("//form[@action='/phpProxy/getJOBolizerResponse_en.php']");
        final HtmlTextInput input = form.getInputByName("url");
        input.setText(vacancyURL);

        HtmlButton button = (HtmlButton) page.getByXPath("/form[@action='/phpProxy/getJOBolizerResponse_en.php']/a[@id=lightboxlink]").get(0);
        HtmlPage page2 = button.click();


        String page2Text = page2.asText();
        System.out.println(page2Text);
    }
}

I figured it out, here is working code:

  HtmlAnchor link = null;
    for (HtmlAnchor anchor : anchors) {
        String str = anchor.asText();
        if (anchor.asText().equals("Start"))
           link = anchor;
    }
    HtmlPage page2 = link.click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM