2

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

1 Answer 1

6

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();
Sign up to request clarification or add additional context in comments.

2 Comments

When you get the from, you use //form (Selects nodes in the document from the current node that match the selection no matter where they are). However, when you try to get anchor, you use /form (Selects from the root node). I don't know structure of your entire form, but that might be issue. In the meantime, could you try this: HtmlAnchor hyperElm = (HtmlAnchor)form.getFirstByXPath("//a[@id=lightboxlink]");
This worked perfectly for me. I don't know how long it would've taken me to figure this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.