Search code examples
javajavascripthtmlhtmlunit

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. enter image description here

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);
    }
}

Solution

  • 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();