2012-07-16 33 views
0

我有一個URL列表,我需要拍攝它們的快照。 我正在使用cmd運行selenium服務器並在eclipse中運行以下代碼。在java中使用selenium拍攝不同網頁的快照

package com.example.tests; 

import com.thoughtworks.selenium.DefaultSelenium; 
import com.thoughtworks.selenium.Selenium; 

public class MainClass { 


    void func(String url, String file) 
    { 
     Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", url); 
     selenium.start(); 

     selenium.windowMaximize(); 

     selenium.open("/"); 
     selenium.waitForPageToLoad("30000"); 


     System.out.println("laoded\n"); 
    // selenium.wait(); 
     String file1= "C:\\test\\"+file+".png"; 
     String file2= "C:\\test\\"+file+"2.png"; 
     selenium.captureScreenshot(file1); 
     selenium.captureEntirePageScreenshot(file2, ""); 

     selenium.stop(); 

    } 


    public static void main(String[] args) 
    { 

     MainClass demo = new MainClass(); 

     demo.func("http://www.facebook.com","face"); 

     demo.func("www.reddit.com","reddit"); 

    } 
} 

它給 this error

我通過these links去,但無法找到一個解決方案。 請幫助我如何解決這個問題。我對硒很陌生。

回答

0

爲什麼不試試這樣說:

WebDriver driver = new FirefoxDriver(); 
driver.get("http://www.google.com/"); 
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
// Now you can do whatever you need to do with it, for example copy somewhere 
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png")); 

或者在這裏看到此鏈接的一個很好的起點教程:http://vishnuagrawal.blogspot.com/2011/12/selenium-taking-screenshot-of-webpage.html

參考文獻:

+0

謝謝你工作正常,但在IE的情況下,它給這個錯誤http://imgur.com/YYOVy。 – Amit 2012-07-16 16:36:26

+0

沒關係。它現在已經修復了。我在這裏找到了解決方案http://www.testerinyou.blogspot.com/。 – Amit 2012-07-16 17:11:45