How can I take a screenshot with Selenium WebDriver?

后端 未结 30 2615
不知归路
不知归路 2020-11-21 07:48

Is it possible to take a screenshot using Selenium WebDriver?

(Note: Not Selenium Remote Control)

相关标签:
30条回答
  • 2020-11-21 08:23

    JavaScript (Selenium-Webdriver)

    driver.takeScreenshot().then(function(data){
       var base64Data = data.replace(/^data:image\/png;base64,/,"")
       fs.writeFile("out.png", base64Data, 'base64', function(err) {
            if(err) console.log(err);
       });
    });
    
    0 讨论(0)
  • 2020-11-21 08:23

    Java

    public String captureScreen() {
        String path;
        try {
            WebDriver augmentedDriver = new Augmenter().augment(driver);
            File source = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
            path = "./target/screenshots/" + source.getName();
            FileUtils.copyFile(source, new File(path)); 
        }
        catch(IOException e) {
            path = "Failed to capture screenshot: " + e.getMessage();
        }
        return path;
    }
    
    0 讨论(0)
  • 2020-11-21 08:23

    Java

    public void captureScreenShot(String obj) throws IOException {
        File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotFile, new File("Screenshots\\" + obj + "" + GetTimeStampValue() + ".png"));
    }
    
    public String GetTimeStampValue()throws IOException{
        Calendar cal = Calendar.getInstance();
        Date time = cal.getTime();
        String timestamp = time.toString();
        System.out.println(timestamp);
        String systime = timestamp.replace(":", "-");
        System.out.println(systime);
        return systime;
    }
    

    Using these two methods you can take a screen shot with the date and time as well.

    0 讨论(0)
  • 2020-11-21 08:24

    Python

    Each WebDriver has a .save_screenshot(filename) method. So for Firefox, it can be used like this:

    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.get('http://www.google.com/')
    browser.save_screenshot('screenie.png')
    

    Confusingly, a .get_screenshot_as_file(filename) method also exists that does the same thing.

    There are also methods for: .get_screenshot_as_base64() (for embedding in HTML) and .get_screenshot_as_png()(for retrieving binary data).

    And note that WebElements have a .screenshot() method that works similarly, but only captures the selected element.

    0 讨论(0)
  • 2020-11-21 08:24

    You can give a try to AShot API. It is on GitHub.

    Examples of tests.

    0 讨论(0)
  • 2020-11-21 08:25

    Java

    Yes, it is possible. The following example is in Java:

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com/");
    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"));
    
    0 讨论(0)
提交回复
热议问题