selenium webdriver中的屏幕快照问题

selenium webdriver中的屏幕快照问题,selenium,Selenium,我有一个屏幕截图问题。当我拍摄屏幕时,它只拍摄可见屏幕。我想捕获整个页面。下面是我的代码 WebDriver webDriver=getCurrentWebDriver(); WebDriverWaitUtility.waitForPageLoad(WebDriverGUIUtilityContants.WaitTime, webDriver); WebDriverGUIUtility.captureScreenShot(webDriver); 下面是selenium和testng代码,用于获

我有一个屏幕截图问题。当我拍摄屏幕时,它只拍摄可见屏幕。我想捕获整个页面。下面是我的代码

WebDriver webDriver=getCurrentWebDriver();
WebDriverWaitUtility.waitForPageLoad(WebDriverGUIUtilityContants.WaitTime, webDriver);
WebDriverGUIUtility.captureScreenShot(webDriver);

下面是selenium和testng代码,用于获取google页面截图

public class ScreenShot {

    public WebDriver d;
    Logger log;
    @Test
    public void m1() throws Exception
    {   
        try
        {
            d=new FirefoxDriver();
            d.manage().window().maximize();
            d.get("https://www.google.co.in/?gfe_rd=cr&ei=4caQV6fxNafnugTjpIGADg");
            String pagetitle=d.getTitle();
            log = Logger.getLogger(FirefoxDriver.class.getName());
            log.info("logger is launched..");
            log.info("Title name : "+pagetitle);
            d.findElement(By.id("testing")).sendKeys("test");
            d.findElement(By.cssSelector("input.gsfi")).sendKeys("gmail account");
        }
        catch(Exception e)
        {
            System.out.println("something happened, look into screenshot..");
            screenShot();
        }
    }
    public void screenShot() throws Exception
    {
        Files.deleteIfExists(Paths.get("G:\\"+"Test results.png"));
        System.out.println("previous pics deleted...");
        File scrFile = ((TakesScreenshot)d).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile,new File("G:\\"+"Test results.png"));

    }

}如果你使用maven,那么你可以使用AShot来完成你的任务。为此,您需要在pom文件中添加依赖项:

<dependency>
    <groupId>ru.yandex.qatools.ashot</groupId>
    <artifactId>ashot</artifactId>
    <version>1.5.2</version>
</dependency>
但是,如果您不使用maven,请下载ashot jar(版本:1.5.2)文件并将其添加到构建路径中。以下是您的帮助链接:


希望,这可能会对您有所帮助。

@naveen,通常情况下,这会发生在Chrome浏览器上。ChromeDriver能够拍摄可见部分的屏幕截图。因此,这里的概念是使用Java脚本执行器滚动页面,拍摄多个图像,然后将它们组合成一个图像。FirefoxDriver能够拍摄整个屏幕的图像而不会出现问题。这里有一个例子

@Test(enabled=true)
public void screenShotExample() throws IOException{
    //WebDriver driver = new FirefoxDriver();
    System.setProperty("webdriver.chrome.driver", "yourpath to chromeDriver\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.w3schools.com/");
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    JavascriptExecutor jexec = (JavascriptExecutor)driver;
    jexec.executeScript("window.scrollTo(0,0)"); // will scroll to (0,0) position 
    boolean isScrollBarPresent = (boolean)jexec.executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight");
    long scrollHeight = (long)jexec.executeScript("return document.documentElement.scrollHeight");
    long clientHeight = (long)jexec.executeScript("return document.documentElement.clientHeight");
    int fileIndex = 1;
    if(driver instanceof ChromeDriver){
        if(isScrollBarPresent){
            while(scrollHeight > 0){
                File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
                jexec.executeScript("window.scrollTo(0,"+clientHeight*fileIndex++ +")");
                scrollHeight = scrollHeight - clientHeight;
            }
        }else{
            File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
        }
    }else{
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
    }
    // Combine all the .jpg file to single file

    driver.close();
    driver.quit();
}

要合并所有图像文件,您将找到一些帮助。希望这能对您有所帮助。

您使用的是哪种驱动程序(浏览器)?我使用的是mozillahi boss ViewportPastingStrategy(1000)的含义是什么naveen viewpastingstrategy意味着将页面向上滚动1000毫秒,直到页脚结束。该建议包括许多与截图原始问题无关的代码,也没有解决截图整个页面(不仅仅是可见部分)的原始问题
@Test(enabled=true)
public void screenShotExample() throws IOException{
    //WebDriver driver = new FirefoxDriver();
    System.setProperty("webdriver.chrome.driver", "yourpath to chromeDriver\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.w3schools.com/");
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    JavascriptExecutor jexec = (JavascriptExecutor)driver;
    jexec.executeScript("window.scrollTo(0,0)"); // will scroll to (0,0) position 
    boolean isScrollBarPresent = (boolean)jexec.executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight");
    long scrollHeight = (long)jexec.executeScript("return document.documentElement.scrollHeight");
    long clientHeight = (long)jexec.executeScript("return document.documentElement.clientHeight");
    int fileIndex = 1;
    if(driver instanceof ChromeDriver){
        if(isScrollBarPresent){
            while(scrollHeight > 0){
                File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
                jexec.executeScript("window.scrollTo(0,"+clientHeight*fileIndex++ +")");
                scrollHeight = scrollHeight - clientHeight;
            }
        }else{
            File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
        }
    }else{
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
    }
    // Combine all the .jpg file to single file

    driver.close();
    driver.quit();
}