Java htmlunit:getPage的不兼容类型?

时间:2013-09-30 10:21:19

标签: java maven intellij-idea htmlunit

在我的代码中,尝试使用getPage方法时出错。我不确定问题是什么,或许我想看的任何想法?

我已经尝试将“found”下的类添加到我的导入中,但它似乎不存在。这是我第一次发布java问题,所以如果需要更多信息,请告诉我。

错误:   java:不兼容的类型   必需:com.gargoylesoftware.htmlunit.html.HtmlPage   发现:com.gargoylesoftware.htmlunit.Page

我的代码

package com.buth.trabot;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.lang.Object;

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.WebClient;
import static org.junit.Assert.*;


/**
 * Created with IntelliJ IDEA.
 * User: Tyler
 * Date: 9/28/13
 * Time: 7:28 PM
 * To change this template use File | Settings | File Templates.
 */
public class Main {
    public static void main (String [] args) {
        final WebClient webClient = new WebClient();
        final HtmlPage startPage = webClient.getPage("http://htmlunit.sf.net");
        assertEquals("HtmlUnit - Welcome to HtmlUnit", startPage.getTitleText());
    }
}

我的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>MavenFirst</groupId>
    <artifactId>MavenFirst</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>1.14</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>
</project>

2 个答案:

答案 0 :(得分:0)

似乎getPage的返回类型是com.gargoylesoftware.htmlunit.Page。 您的变量属于com.gargoylesoftware.htmlunit.html.HtmlPage

您需要更改变量的类型或添加演员。

答案 1 :(得分:0)

我想你应该用这种方式链接它:

<dependency>
    <groupId>net.sourceforge.htmlunit</groupId>
    <artifactId>htmlunit</artifactId>
    <version>2.12</version>
</dependency>