public static void main(String[] args) throws InterruptedException { DesiredCapabilities sCaps = new DesiredCapabilities(); // Prepare capabilities sCaps.setJavascriptEnabled(true); sCaps.setCapability("takesScreenshot", false); ArrayList<String> cliArgsCap = new ArrayList<String>(); cliArgsCap.add("--web-security=false"); cliArgsCap.add("--ssl-protocol=any"); cliArgsCap.add("--ignore-ssl-errors=true"); cliArgsCap.add("--load-plugins=yes"); sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap); sCaps.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS, new String[]{"--logLevel=INFO","--load-images=false"}); WebDriver driver = new PhantomJSDriver(sCaps); driver.navigate().to("http://www.xxx.com"); System.out.println(driver.getTitle()); Thread.sleep(2000); driver.findElement(By.xpath("//div[@class='list-cont']/ul[1]/li/a")).click(); System.out.println(driver.getWindowHandles()); Set<String> handles = driver.getWindowHandles(); String[] temp = new String[handles.size()]; int i = 0; for (Iterator<String> it = handles.iterator(); it.hasNext(); i++) { temp[i] = (String) it.next(); } driver.switchTo().window(temp[1]); System.out.println(driver.getTitle()); driver.quit(); }
不加載圖片,最基礎的調用,執行速度比Firefox略快 java