2013-03-19 59 views
0

我想用Ghost驅動程序解析Google搜索結果頁面,爲此我需要解析第二個搜索結果頁面。因此,使用Java腳本,我試圖通過搜索結果頁面的源代碼(html)並通過單擊(使用Ghost驅動程序)下方的頁碼進行導航。然後它拋出下面的異常。嵌入JavaScript解析並點擊硒鬼驅動程序中的網頁

import java.util.List; 

import org.openqa.selenium.By; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; 

driver = new HtmlUnitDriver(); 

    JavascriptExecutor jse = (JavascriptExecutor)driver; 
    jse.executeScript("document.querySelectorAll('#foot span div table tbody tr td a')[2].click();"); 




Exception in thread "main" java.lang.UnsupportedOperationException: Javascript is not enabled for this HtmlUnitDriver instance 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.getPageToInjectScriptInto(HtmlUnitDriver.java:503) 
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.executeScript(HtmlUnitDriver.java:458) 
    at ATest.main(ATest.java:46) 

回答

1

這不是使用GhostDriver;這是使用HtmlUnit。你有兩個選擇。您可以爲HtmlUnit啓用JavaScript,這可以通過驅動程序的所需功能來完成,也可以使用GhostDriver,它是PhantomJS項目的一部分。在您的Java代碼中,您將創建一個PhantomJSDriver對象,該對象將用於導航以操縱生成的頁面。