0

我需要捕获 IE 浏览器的完整页面。我正在使用网络驱动程序。请帮助我。

下面的代码仅用于捕获当前窗口。

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

2 回答 2

1

您可以在截屏之前将浏览器更改为全屏。这是代码:

private void changeToFullScreen() {
    try {
        Robot r;
        r = new Robot();
        r.keyPress(java.awt.event.KeyEvent.VK_F11);
        r.keyRelease(java.awt.event.KeyEvent.VK_F11);
        driver.sleep(2);
    } catch (AWTException e) {
        log.error("It was not possible to maximize", e)
        driver.manage().window().maximize();
    }
}

如果您不想更改屏幕大小,您可以获取浏览器高度并在向下滚动到底部时截取屏幕截图(可以使用 Robot 完成):

int numberOfScrolls = pageHeight / browserHeight;
for(int i = 0 ; i < numberOfScrolls ; i++){
   takeScreenshot();
   scrollDown(browserHeight);
}
concatenateScreenshots();
于 2014-06-19T15:37:51.577 回答
0
import java.io.File;
import javax.imageio.ImageIO;
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 TakeScreenshotWithScroll {
    static WebDriver driver;
    public static void main(String args[]) throws Exception{
        String key = "webdriver.gecko.driver";
        String value = "driver/geckodriver.exe";
        driver = new FirefoxDriver();
        System.setProperty(key, value);
        driver.get("ENTER THE URL HERE");
        Thread.sleep(2000);
        Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
        ImageIO.write(fpScreenshot.getImage(),"PNG",new File("D:///FullPageScreenshot.png"));
    }
}

这是aShot Jar文件的下载链接

于 2017-12-06T04:29:04.053 回答