Question: I have searched a lot for this but couldn't find a solution. Here's [a similar question](https://stackoverflow.com/questions/10848900/how-to-take-partial-screenshot-frame-with-selenium-webdriver) with a possible solution in java.

Is there a similar solution in Python?

Answer: 
I wrote this useful python3 function.

```
from base64 import b64decode
from wand.image import Image
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.action_chains import ActionChains
import math

def get_element_screenshot(element: WebElement) -> bytes:
    driver = element._parent
    ActionChains(driver).move_to_element(element).perform()  # focus
    src_base64 = driver.get_screenshot_as_base64()
    scr_png = b64decode(src_base64)
    scr_img = Image(blob=scr_png)

    x = element.location["x"]
    y = element.location["y"]
    w = element.size["width"]
    h = element.size["height"]
    scr_img.crop(
        left=math.floor(x),
        top=math.floor(y),
        width=math.ceil(w),
        height=math.ceil(h),
    )
    return scr_img.make_blob()

```

It returns png image of displayed element as bytes.
Limitation: element must fit in viewport.  

You must install wand module to work with it.
I converted @randomphobia's answer into a function. I also used @bummis' suggestion of using `location_once_scrolled_into_view` instead of `location` in order to generalize no matter the size of the page.

```py
from selenium import webdriver
from PIL import Image
from io import BytesIO

def take_screenshot(element, driver, filename='screenshot.png'):
  location = element.location_once_scrolled_into_view
  size = element.size
  png = driver.get_screenshot_as_png() # saves screenshot of entire page

  im = Image.open(BytesIO(png)) # uses PIL library to open image in memory

  left = location['x']
  top = location['y']
  right = location['x'] + size['width']
  bottom = location['y'] + size['height']

  im = im.crop((left, top, right, bottom)) # defines crop points
  im.save(filename) # saves new cropped image

```

Here's a gist: <https://gist.github.com/WittmannF/b714d3ceb7b6a5cd50002f11fb5a4929>