簡體   English   中英

使用Selenium 3 WebDriver捕獲網頁內容的全屏快照

[英]Capturing full screen shot of webpage content using selenium 3 webdriver

我正在嘗試對網頁進行完整的內容截屏。 但我只能通過以下代碼獲得基於視圖的屏幕截圖。 瀏覽器是Firefox,我正在使用SELENIUM 3 Web驅動程序。

File scrFile5 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            try {

                FileUtils.copyFile(scrFile5, new File("C:\\test.jpg"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

我怎么能達到同樣的目的。

您可能要使用RemoteWebDriver界面。 您正在使用Firefox或IE嗎? 見下文

File screenshotFile = null;
            try {
                // This checks to make sure we're not running an
                // incompatible driver to take a screenshot (e.g.
                // screenshots are not supported by the HtmlUnitDriver)

                //the following line will throw a ClassCastException if the current driver is not a browser typed driver
                RemoteWebDriver compatibleDriver = (RemoteWebDriver) driver;

                screenshotFile = ((TakesScreenshot) compatibleDriver).getScreenshotAs(OutputType.FILE);
            } catch (ClassCastException e) {
                //this driver does not support screenshots.
                // this should get logged as a warning -- this only means the driver cannot be of type RemoteWebDriver

            } finally {
                if (screenshotFile == null) {
                    System.out.println("This WebDriver does not support screenshots");
                    return;  //get us outa here
                }
            }

            try {

                String pathString = "some path of your choice"
                File path = new File(pathString);

                if (!path.exists())
                    path.mkdirs();

                stringPath.concat("/someFileName.png");

                File newFileLocation = new File(pathString);
                System.out.println("Full path to screenshot: " + newFileLocation.getAbsolutePath());

                FileUtils.copyFile(screenshotFile, newFileLocation);
            } catch (IOException e) {
                e.printStackTrace();
            }

Selenium瀏覽器驅動程序是一個老問題。

試試這個工具:aShot

您可以在https://github.com/yandex-qatools/ashot中找到它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM