简体   繁体   English

如何使用 Java Selenium 抓取远程网站

[英]How to capture a remote website using Java Selenium

can anyone tell me how I can capture a webpage using Java Selenium?谁能告诉我如何使用 Java Selenium 捕获网页? With an example....举个例子....

See here: Capturing screenshots from remote Selenium RC .请参阅此处: 从远程 Selenium RC 捕获屏幕截图

In essence:本质上:

"To solve this you can use the captureScreenshotToString and captureEntirePageScreenshotToString commands, which return a Base64 encoded String of the screenshot, which you can then decode and save to disk on your testrunner machine." “要解决这个问题,您可以使用 captureScreenshotToString 和 captureEntirePageScreenshotToString 命令,它们返回屏幕截图的 Base64 编码字符串,然后您可以将其解码并保存到测试运行器机器上的磁盘。”

public static void getSnapShot(WebDriver driver, String event) {
     {
        try {
            File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            BufferedImage originalImage = ImageIO.read(scrFile);
            //int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
            BufferedImage resizedImage = CommonUtilities.resizeImage(originalImage, IMG_HEIGHT, IMG_WIDTH);
            ImageIO.write(resizedImage, "jpg", new File(path + "/"+ testCaseId + "/img/" + index + ".jpg"));
            Image jpeg = Image.getInstance(path + "/" + testCaseId + "/img/"+ index + ".jpg");
            jpeg.setAlignment(Image.MIDDLE);

            ++index;
        } catch (Exception  e) {
            e.printStackTrace();
        }
    }
}

I like to use PhantomJS driver for taking screenshots.我喜欢使用 PhantomJS 驱动程序来截屏。

public class Test {

    public static void main(String[] args) {
        //PhantomJS headless driver
        File file = new File("D:\\Webdriver\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
        System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
        WebDriver driver = new PhantomJSDriver();

        //Set Size here
        driver.manage().window().setSize(new Dimension(1600,900));

        //To wait until the element get visible or invisible.
        WebDriverWait wait = new WebDriverWait(driver, 25);

        //To access url.
        driver.get("https://www.google.co.in");

        //For wait until the element get visible.
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lst-ib")));

        File shot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(shot, new File("D:\\Webdriver\\Capture.jpg"));
    }

}

I think this is what you are looking for.我想这就是你要找的。 But try to be mores specific if it is not.但如果不是,请尝试更具体。

captureEntirePageScreenshot ( filename,kwargs ) Saves the entire contents of the current window canvas to a PNG file. captureEntirePageScreenshot ( filename,kwargs ) 将当前窗口画布的全部内容保存为 PNG 文件。 Contrast this with the captureScreenshot command, which captures the contents of the OS viewport (ie whatever is currently being displayed on the monitor), and is implemented in the RC only.将此与 captureScreenshot 命令形成对比,该命令捕获操作系统视口的内容(即当前显示在监视器上的任何内容),并且仅在 RC 中实现。 Currently this only works in Firefox when running in chrome mode, and in IE non-HTA using the EXPERIMENTAL "Snapsie" utility.目前,这仅在 Firefox 中以 chrome 模式运行时有效,在 IE 非 HTA 中使用实验性“Snapsie”实用程序。 The Firefox implementation is mostly borrowed from the Screengrab! Firefox 的实现主要是从 Screengrab 借来的! Firefox extension.火狐扩展。 Please see http://www.screengrab.org and http://snapsie.sourceforge.net/ for details.有关详细信息,请参阅http://www.screengrab.orghttp://snapsie.sourceforge.net/

 Arguments: * filename - the path to the file to persist the screenshot as. No

filename extension will be appended by default.默认情况下将附加文件扩展名。 Directories will not be created if they do not exist, and an exception will be thrown, possibly by native code.如果目录不存在,则不会创建目录,并且可能由本机代码引发异常。 * kwargs - a kwargs string that modifies the way the screenshot is captured. * kwargs - 修改截屏方式的 kwargs 字符串。 Example: "background=#CCFFDD" .示例: "background=#CCFFDD" 。 Currently valid options:当前有效的选项:

 background the background CSS for the HTML document. This may be useful

to set for capturing screenshots of less-than-ideal layouts, for example where absolute positioning causes the calculation of the canvas dimension to fail and a black background is exposed (possibly obscuring black text).设置用于捕获不太理想的布局的屏幕截图,例如绝对定位导致画布尺寸计算失败并暴露黑色背景(可能会遮挡黑色文本)。

It takes full page screenshot of chrome webpage.它需要 chrome 网页的全页截图。

System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe"); 

ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);   
String baseUrl = "https://www.google.co.in";

driver.get(baseUrl);        
String fullscreen =Keys.chord(Keys.F11);
driver.findElement(By.cssSelector("body")).sendKeys(fullscreen);

TakesScreenshot scrShot =((TakesScreenshot)driver);  
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);   
File DestFile=new File("F://test.png");  
FileUtils.copyFile(SrcFile, DestFile);   
driver.close();

I see a lot of answers explaining screenshots, but just incase you are asking how to get the entire source of the page use the following method:我看到很多解释屏幕截图的答案,但万一您询问如何获取页面的整个源代码,请使用以下方法:

  String pageSource = driver.getPageSource();

Here is a runnable example. 这是一个可运行的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM