WebElement#getScreenShotAs(OutputType.File) not working

前端 未结 4 546
时光取名叫无心
时光取名叫无心 2021-01-21 05:08

I am trying to use WebElement#getScreenShotAs(OutputType.FILE) feature added in selenium webdriver 2.47.0 version on Firefox Browser Code

public         


        
相关标签:
4条回答
  • 2021-01-21 05:19

    Dont Import these lib from Suggestions,

    import org.eclipse.jetty.server.Response.OutputType;

    import org.seleniumhq.jetty9.server.Response.OutputType;

    Import these lib.

    import org.openqa.selenium.OutputType;

    0 讨论(0)
  • 2021-01-21 05:25

    The real reason for the error is that many / most WebDriver implementations do not actually support element-based screenshots, despite WebElement extending TakesScreenshot since 2.47.0. Perhaps someday this will change.

    If you want screenshots you need to do them at the whole-browser level, in which case - as other answers have it - you need to pass the WebDriver instance.

    File ssFile = ((TakesScreenshot)(driver)).getScreenshotAs(OutputType.FILE);
    

    Strictly speaking you should probably do the following, since not all Drivers are guaranteed to support screenshots, e.g. HtmlUnitDriver.

    if (!(getDriver() instanceof TakesScreenshot)) {
        File ssFile = ((TakesScreenshot)(driver)).getScreenshotAs(OutputType.FILE);
        // ...
    }
    

    There are alternate solutions for single-element screenshots, but they inevitably involve cropping of the full-browser screenshot. See, for example: https://stackoverflow.com/a/13834607/954442

    Update: just to clarify that this is not a bug, it's that although element screenshots are a part of the W3C WebDriver spec, different browsers have different levels of compliance/coverage, and as far as I know this feature is only supported by Microsoft Edge.

    0 讨论(0)
  • 2021-01-21 05:32

    It should be something like this.

    File screenS = ((TakesScreenshot)(driver)).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screenS, new File("C:\\akshay\\del\\screenshots\\1.jpg"));
    

    replace the above code with

    element.getScreenshotAs(OutputType.FILE);
            File destination=new File("Image.png");
            FileUtils.copyFile(null, destination);
    
    0 讨论(0)
  • 2021-01-21 05:40

    The getScreenShotAs() method is not defined as part of the WebElement interface. Rather, it is included as part of the TakesScreenshot interface. If you want to take a screenshot and save it to a file, try the following:

    File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    
    0 讨论(0)
提交回复
热议问题