1

对于以下版本(JDK:8,JRE:8,HtmlUnit:2.17 和 Selenium Webdriver:2.46),我的这段代码工作得很好。当我说好的时,这意味着我能够下载完整的网络内容(每一行)。

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);

没有任何效果。我可以添加哪些其他内容,请提出建议。

4

1 回答 1

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();         
    }      
}

谢谢大家。

于 2015-06-10T10:11:52.943 回答