WebElement#getScreenShotAs(OutputType.File) not working

前端 未结 4 547
时光取名叫无心
时光取名叫无心 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: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.

提交回复
热议问题