调用javascript window.scrollTo

时间:2015-07-08 11:58:39

标签: javascript java javafx webkit ui4j

我正在使用ui4j测试网站。当我执行

page.executeScript("window.scrollTo(0,document.body.scrollHeight)");

滚动到页面的末尾我得到以下NullPointerException:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at com.sun.javafx.webkit.theme.ScrollBarThemeImpl.getThumb(ScrollBarThemeImpl.java:400)
    at com.sun.javafx.webkit.theme.ScrollBarThemeImpl.thumbPosition(ScrollBarThemeImpl.java:284)
    at com.sun.javafx.webkit.theme.ScrollBarThemeImpl.getThumbPosition(ScrollBarThemeImpl.java:380)
    at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
    at com.sun.webkit.WebPage.executeScript(WebPage.java:1427)
    at javafx.scene.web.WebEngine.executeScript(WebEngine.java:948)
    at com.ui4j.webkit.spi.WebKitJavaScriptEngine.executeScript(WebKitJavaScriptEngine.java:26)
    at com.ui4j.webkit.browser.WebKitPage.executeScript(WebKitPage.java:231)
    at com.ui4j.webkit.browser.WebKitPage$ByteBuddy$sjifLXwz.executeScript$accessor$6vHNXtoG(Unknown Source)
    at com.ui4j.webkit.browser.WebKitPage$ByteBuddy$sjifLXwz$auxiliary$0UdQY8PH.call(Unknown Source)
    at com.ui4j.webkit.proxy.WebKitProxy$CallableExecutor.run(WebKitProxy.java:46)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$51/1744951114.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/929850074.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 

我在mac上用ui4j 2.0和1.2.0试了一下。 JDK版本1.8.0_45。

ui4j上是否有一些配置丢失,无法执行'window.scrollTo'?

我按以下方式加载网页:

BrowserEngine engine = BrowserFactory.getWebKit();
Page page = engine.navigate( url );

我是否遗漏了某些内容,或者ui4j下的'window.scrollTo'是否有解决方法以避免NullPointerException?

1 个答案:

答案 0 :(得分:1)

page.executeScript()之前调用 page.show()

import static com.ui4j.api.browser.BrowserFactory.getWebKit;

import com.ui4j.api.browser.Page;

public class Sample {

    public static void main(String[] args) {
        Page page = getWebKit().navigate("http://stackoverflow.com");
        page.show();
        page.executeScript("window.scrollTo(0,document.body.scrollHeight)");
    }
}