2015-06-10 44 views
1

對於以下版本(JDK:8,JRE:8,HtmlUnit:2.17和Selenium Webdriver:2.46),我的代碼工作得很好。當我說好時,意味着我可以下載完整的網頁內容(每一行)。使用Selenium Webdriver和HtmlUnit下載網頁內容

package mypackage; 
import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver;  
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

import com.gargoylesoftware.htmlunit.BrowserVersion; 
public class HtmlUnitTest {    
public static void main(String[] args) { 
WebDriver driver = new FirefoxDriver(); // Working fine 
String baseUrl = "h t t p s :// fourseasons . wd3 . myworkdayjobs . com/search/jobs"; 
driver.get(baseUrl); 
WebDriverWait myWaitVar = new WebDriverWait(driver, 20); 
try{ 
    myWaitVar.until(ExpectedConditions.visibilityOfElementLocated(By.id("wd-FacetedSearchResult-ResultsPnl-facetSearchResult"))); 
}catch(Exception ex){ 
    ex.printStackTrace(); 
} 
String content=driver.getPageSource(); 
System.out.println(content); 
driver.close();   
}  

}

但是,當我駕駛者改變HtmlUnitDriver,它不會下載的全部內容。

WebDriver driver = new HtmlUnitDriver(); 

我的嘗試則:

1. WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38); 
2. WebDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME); 
3. WebDriver driver = new HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER_11); 

沒有什麼工作。 我可以添加什麼額外的東西,請建議。

+0

沒有@LaurentiuL。 ,它不重複。我沒有獲得全部內容,這是我的問題。上面的鏈接(由你提到)有一個xpath的問題。我想我必須設置更多的參數或做額外的事情來獲取內容。我是新的硒web驅動程序,所以請建議做什麼? – Shashank

回答

1

我得到了答案的傢伙。我啓用了JavaScript,它的工作。所以新代碼是這樣的:

public class HtmlUnitTest {    
    public static void main(String[] args) { 
     HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38); 
     driver.setJavascriptEnabled(true); 
     String baseUrl = "h t t p s :// fourseasons . wd3 . myworkdayjobs . com/search/jobs"; 
     driver.get(baseUrl); 
     WebDriverWait myWaitVar = new WebDriverWait(driver, 20); 
     try{ 
      myWaitVar.until(ExpectedConditions.visibilityOfElementLocated(By.id("wd-FacetedSearchResult-ResultsPnl-facetSearchResult"))); 
     }catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
     String content=driver.getPageSource(); 
     System.out.println(content); 
     driver.close();   
    }  
} 

謝謝大家。