最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Java HtmlUnit click on anchor - Stack Overflow

programmeradmin0浏览0评论

I am trying to make java app using htmlunit to get info from 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 = "";
    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 am trying to make java app using htmlunit to get info from http://www.jobolizer. 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.";
    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);
    }
}
Share edited Sep 14, 2014 at 10:37 PBA asked Sep 12, 2014 at 14:48 PBAPBA 4252 gold badges9 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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();
发布评论

评论列表(0)

  1. 暂无评论