2017-05-09 108 views
2

我試圖用對的HtmlUnit加載的Playstation Store頁面,但看起來像它加載與「正在加載...」文本(和一些JavaScript)空白頁的一切。 我用下面的配置,使工作的HtmlUnit,但絕望(其科特林):阻止HtmlUnit加載PSN商店頁面?

мая 09, 2017 4:08:22 PM com.gargoylesoftware.htmlunit.html.HtmlScript isExecutionNeeded 
WARNING: Script is not JavaScript (type: application/json, language:). Skipping execution. 
мая 09, 2017 4:08:22 PM com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController processSynchron 
INFO: Re-synchronized call to https://sonynetworkentertainment.112.2o7.net/b/ss/snestorewebloadglobal/1/chidv1/s75296982536092?AQB=1&ndh=1&t=9%2F5%2F2017%2016%3A8%3A22%202%20-180&ts=1494335302&vid=c61f4752-adfd-84d1-728c-187350f9aa37&pageName=web%3Aloading_start&v1=D%3DpageName&g=https%3A%2F%2Fstore.playstation.com%2F&r=&v2=xx-xx&ch=web%3Aloading_start&c68=D%3Dg&c72=web&v72=web&cc=USD&ce=UTF-8&server=web&events=event1&AQE=1 
мая 09, 2017 4:08:22 PM com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController processSynchron 
INFO: Re-synchronized call to https://store.playstation.com/kamaji/api/chihiro/00_09_000/geo 

的問題是:什麼停止

@Test 
@Throws(Exception::class) 
fun homePage() { 
    val webClient = WebClient(BrowserVersion.INTERNET_EXPLORER).apply { 
     ajaxController = NicelyResynchronizingAjaxController() 
     options.isUseInsecureSSL = true 
     options.isThrowExceptionOnScriptError = false 
     options.isJavaScriptEnabled = true 
     options.isCssEnabled = true 
     options.isRedirectEnabled = true 
     options.isThrowExceptionOnFailingStatusCode = false 
     options.isUseInsecureSSL = true 
     options.isDownloadImages = true 
     cookieManager.isCookiesEnabled = true 
     waitForBackgroundJavaScript(10000) 
     waitForBackgroundJavaScriptStartingBefore(10000) 
    } 

    val page = webClient.getPage<HtmlPage>("https://store.playstation.com/") 
    Thread.sleep(10000) 
    assertFalse(page.asXml().contains("Loading")) 
} 

我不加載頁面時看到任何具體的錯誤HtmlUnit從加載頁面?我試圖自己弄明白,但我唯一的想法是,它可能是針對無頭瀏覽器的某種防禦或HtmlUnit不支持的非常重的JS。但對於例如

https://account.sonyentertainmentnetwork.com

可以沒有任何麻煩地打開。

回答

0

這就是所謂的SPA - Single Page Application。一般來說,SPA只有基本的標記,一個容器,整個用戶界面是用框架動態呈現的,如ReactAngular

https://store.playstation.com剩下剝離腳本和樣式後是:

<div id="waitAppLoading"> 
    <div class="waitHorizon"> 
    <div class="centerBox"> 
     <div class="logoCtnr"></div> 
     <div class="textBox"><div class="spinCtnr"></div><div id="appLoadingMsg"></div></div> 
     <div class="startupErr"></div> 
    </div> 
    </div> 
</div> 
<div id="appRoot" class="hidden"></div> 
<div id="lockdownScreen"></div> 
<div id="global-wait"> 
    <div class="waitHorizon"> 
    <div class="waitContainer"> 
     <div class="sq1"></div> 
     <div class="sq2"></div> 
     <div class="sq3"></div> 
     <div class="sq4"></div> 
     <div class="sq5"></div> 
     <div class="sq6"></div> 
    </div> 
    </div> 
    <div id="global-ps-loader"> 
    </div> 
</div> 
<div id="notifierCtnr" class="mainCol"><div id="notifier-box"></div></div> 
<div id="storeNotAvail"></div> 
<div class="dimToolEl dimToolElProdTitle"></div> 
<div class="dimToolEl dimToolElProdSubTitle"></div> 
<div id="transact-iframe-container"> 
    <iframe id="transact-iframe"></iframe> 
</div> 

正如你看到的,這裏沒有內容,只爲應用線框。 Web客戶端不能完全模擬瀏覽器,也不會執行該腳本。這就是爲什麼你看到一個空的頁面。

+0

謝謝你,我知道這是SPA。但是HtmlUnit可以處理JS請求,它實際上可以在一些SPA中工作。看起來我已經用錯了方式等待JS執行 – Ilya

0

至少這java代碼在這裏工作。我在真正的FF中獲得了語言選擇對話框。 我使用最新的HtmlUnit代碼。這通常是一個好主意。

String url = "https://store.playstation.com/"; 

    try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52)) { 
     final HtmlPage page = webClient.getPage(url); 
     webClient.waitForBackgroundJavaScript(1000 * 10); 

     System.out.println("----------------"); 
     System.out.println(page.asText()); 
     System.out.println("----------------"); 

     HtmlElement btn = page.querySelector(".btn"); 
     System.out.println(btn.asXml()); 
     System.out.println("----------------"); 
    } 

並請刪除通話

waitForBackgroundJavaScript(10000) 
    waitForBackgroundJavaScriptStartingBefore(10000) 

從您的設置代碼。這些方法不設置任何選項;他們在執行時正在等待。

+0

哦!所以應該調用這些方法,而不是Thread.sleep()。然後,可能會有助於克服「加載」屏幕。將嘗試,謝謝 – Ilya