HtmlUnit - 代码适用于我自己的系统,而不是我的VPS?

时间:2016-08-28 19:23:14

标签: java htmlunit

我编写了一些代码来自动完成我想要执行的某项任务,并且它在我的计算机上运行正常。由于此任务必须每小时执行一次,因此我在我的VPS(亚马逊)上运行它。 但是在我的VPS上运行代码会给我带来大部分错误,我无法在自己的计算机上重现这些错误。在我自己的计算机上,我可以根据需要和每次正常运行它运行它。

任何人都可以想到发生这种情况的可能原因吗?

我的代码是:

public final class Test {

    // XPath elements
    private static final String USERNAME_FIELD = "//*[@id=\"username\"]";
    private static final String PASSWORD_FIELD = "//*[@id=\"password\"]";
    private static final String SIGN_IN_BUTTON = "//*[@id=\"loginForm\"]/fieldset[2]/button";
    private static final String RUN_TEST_BUTTON = "/html/body/div[1]/div/header/div/div/button/span";

    // User credentials
    private static final String USERNAME = "my-username";
    private static final String PASSWORD = "my-password";

    // TestObject URL's
    private static final String LOGIN_URL = "url";
    private static final String RUN_TEST_URL = "my-test-url;

    private static final int TIME_OUT = 10000;

    public static void main(String[] args) throws IOException {

        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

        try (final WebClient client = new WebClient(BrowserVersion.CHROME)) {
            client.setAjaxController(new NicelyResynchronizingAjaxController());
            client.setCssErrorHandler(new SilentCssErrorHandler());

            client.getOptions().setCssEnabled(true);
            client.getOptions().setRedirectEnabled(true);
            client.getOptions().setAppletEnabled(false);
            client.getOptions().setJavaScriptEnabled(true);
            client.getOptions().setPopupBlockerEnabled(true);
            client.getOptions().setTimeout(TIME_OUT);
            client.getOptions().setThrowExceptionOnFailingStatusCode(false);
            client.getOptions().setThrowExceptionOnScriptError(false);
            client.getOptions().setPrintContentOnFailingStatusCode(true);
            //client.setHTMLParserListener(HTMLParserListener.LOG_REPORTER);
            client.waitForBackgroundJavaScript(TIME_OUT/2);

            HtmlPage page = client.getPage(LOGIN_URL);
            HtmlTextInput user = (HtmlTextInput) page.getByXPath(USERNAME_FIELD).get(0);
            HtmlPasswordInput pass = (HtmlPasswordInput) page.getByXPath(PASSWORD_FIELD).get(0);
            HtmlButton login = (HtmlButton) page.getByXPath(SIGN_IN_BUTTON).get(0);

            user.setValueAttribute(USERNAME);
            pass.setValueAttribute(PASSWORD);

            page = login.click();

            page = client.getPage(RUN_TEST_URL);
            HtmlSpan runTest = (HtmlSpan) page.getByXPath(RUN_TEST_BUTTON).get(0); // this is the place where it gives an "out-of-bound exception"

            page = runTest.click();

            client.close();
        }
    }
}

错误:

  

net.sourceforge.htmlunit.corejs.javascript.Undefined@2c543bc 5       线程" main"中的例外情况       java.lang.IndexOutOfBoundsException:Index:0,Size:0           at java.util.ArrayList.rangeCheck(ArrayList.java:653)           at java.util.ArrayList.get(ArrayList.java:429)           at eu.theappfactory.testobject.driver.TestObjectDriver.main(Tes tObjectDriver.java:7 1)

0 个答案:

没有答案