如何使用Selenium和Java获取完整网页的屏幕截图?

如何使用Selenium WebDriver制作完整整个网页的截图?

我的Selenium WebDriver java代码如下:

System.setProperty("webdriver.chrome.driver","/home/alex/Downloads/chromedriver_linux64/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("http://google.com"); File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File(RESULT_FILENAME)); 

有任何想法吗?

简短的回答是不,你不能 (详细原因如下)。 但是,您可以做的是充分利用设备的(监视器) 视口 。

因此,使用ChromeOptions()启动浏览器实例(驱动程序),这是设置ChromeDriver特定function的方法。 我们可以做到以下几点:

  • 最大化窗口(使用--start-maximized开关);
  • 全屏显示(Windows上的F11键,Mac上的Control+Cmd+F ,使用--start-fullscreen开关)。
  • 注意:可以在此处找到完整的Chrome命令行开关列表。

您的代码应如下所示:

 // Setting your Chrome options (Desired capabilities) ChromeOptions options = new ChromeOptions(); options.add_argument('--start-maximized'); options.add_argument('--start-fullscreen'); // Creating a driver instance with the previous capabilities WebDriver driver = new ChromeDriver(options); // Load page & take screenshot of full-screen page driver.get("http://google.com"); File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 

现在关于整页问题,所有驱动程序( chromedrivergeckodriverIEDriverEdgeDriver等)都在实施WebDriver W3C标准。 因此,它们取决于WebDriver团队想要公开不同function的function的方式,在我们的案例中, “截取屏幕截图” 。

如果您阅读了该定义,则会明确说明以下内容:

Take Screenshot命令获取顶级浏览上下文的VIEWPORT的屏幕截图。

遗产方面,一些驱动程序能够生成整页截图( 在这里阅读更多相关内容),如旧的FirefoxDriver,IEDriver等。现在不再是这种情况了,因为它们现在都实现了(在信中)WebDriver W3C标准。

我找到了一个教程http://www.softwaretestingmaterial.com/how-to-capture-full-page-screenshot-using-selenium-webdriver/对于maven用户:记得从https://mvnrepository.com/添加依赖关系artifact / ru.yandex.qatools.ashot / ashot当我测试这个脚本时,我有大图片,无法在浏览器中打开(太大或损坏)。

所以也许有人知道任何脚本,它将页面集中在上次使用的定位器上?

对于某些页面,我需要截取页面的开头和结尾的截图。 所以我用2个截图:

 public static String javaIoTmpDir = System.getProperty("java.io.tmpdir"); //tmp dir //create screenshot driver.manage().window().fullscreen(); //For large pages - body over 850 px highh - take addition screenshot of the end of page if (driver.findElements(By.id("panel_body")).size()>0) { WebElement body = driver.findElement(By.id("panel_body")); int bodyHight = body.getSize().getHeight(); if (bodyHight > 850) { Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_END); robot.keyRelease(KeyEvent.VK_END); Thread.sleep(1000); File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); String timePictureEnd = javaIoTmpDir+"\\scr_"+String.valueOf(System.currentTimeMillis())+getClass().toString().substring(getClass().toString().lastIndexOf(".qa.")+3)+".png"; FileUtils.copyFile(scrFile, new File(timePictureEnd)); robot.keyPress(KeyEvent.VK_HOME); //back to top page for next screen robot.keyRelease(KeyEvent.VK_HOME); Thread.sleep(1000); } } String timePicture = javaIoTmpDir+"\\scr_"+String.valueOf(System.currentTimeMillis())+getClass().toString().substring(getClass().toString().lastIndexOf(".qa.")+3)+".png"; File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File(timePicture)); 

您可以使用Phantomjs来实现它。

这是我的java代码:

 public class PhantomjsTest { public static void main(String[] args) { try { long start=System.currentTimeMillis(); //The page you want to screenshot String targetUrl="https://www.iqiyi.com/"; //File path String targetImg="iqiyi.png"; String command = "/Users/hetiantian/SoftWares/phantomjs/bin/phantomjs /Users/hetiantian/SoftWares/phantomjs/examples/rasterize.js "+targetUrl + " " +targetImg; Process p = Runtime.getRuntime().exec(command); p.waitFor(); System.out.println((System.currentTimeMillis()-start)); } catch (Exception e) { e.printStackTrace(); } } } 

因此,您可以获得Phantomjs的完整网页截图。 当您使用Phantomjs获取完整的屏幕截图时,您需要先下载Phantomjs。 然后运行Phantomjs脚本进行截图。 更多关于它你可以参考phantomjs.org 。

 File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File("c:\\RESULT_FILENAME.png")); 

试试这个

Interesting Posts