需要使用webdriver获取www.flipkart.com的完整网页截图

时间:2016-10-08 06:13:07

标签: selenium selenium-webdriver webdriver

我需要使用网络驱动程序捕获www.flipkart.com的整个网页。

我为此编写了以下代码。但它不起作用。请建议。

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

public class ScreenShot {

    public static WebDriver driver = null;

    public static void main(String[] args) throws IOException, InterruptedException {
                System.setProperty("webdriver.gecko.driver","C:\\Eclipse\\Drivers\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();

        driver.get("https://www.flipkart.com/");
        Thread.sleep(10000);


        //Take the screenshot of the entire home page and save it to a png file
        Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);

        //Screenshot screenshot = new AShot().takeScreenshot(driver, driver.findElement(By.xpath(".//*[@id='container']")));
        ImageIO.write(screenshot.getImage(), "PNG", new File("C:\\Users\\Vishvambruth JT\\Desktop\\home.png"));

        driver.quit();
    }   
}

1 个答案:

答案 0 :(得分:0)

使用以下代码尝试一次。

public class Flipcart { 
    public WebDriver d;
    Logger log;
    @Test
    public void m1() throws Exception
    {   
        try
        {
            d=new FirefoxDriver();
            d.manage().window().maximize();
            d.get("https://www.flipkart.com/");
            String pagetitle=d.getTitle();

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

}