1

enter image description here

I can screenshot a whole page using Firefox.get_screenshot_as_file('2.png'),but when I screenshot a web element using passage.screenshot('1.png'),it alway raise this exception:

selenium.common.exceptions.WebDriverException: Message: Unrecognized command: GET /session/284283fa-53fc-4b33-b329-e6e888dbdcb0/screenshot/{35834cf1-c9c7-4129-99b1-24f30c6b56e6}
Louise
  • 91
  • 1
  • 2
  • 9

2 Answers2

6

You're getting this exception because you cannot take a screenshot of just an element in selenium without some third-party libraries or your own code to handle this. See This stackoverflow post

Which uses a library called PIL to do it:

from selenium import webdriver
from PIL import Image

fox = webdriver.Firefox()
fox.get('https://stackoverflow.com/')

# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
fox.save_screenshot('screenshot.png') # saves screenshot of entire page
fox.quit()

im = Image.open('screenshot.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('screenshot.png') # saves new cropped image
Community
  • 1
  • 1
Mobrockers
  • 2,128
  • 1
  • 16
  • 28
  • it is useful.But I read selenium API [link](http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement),web element has this method `element.screenshot(‘/Screenshots/foo.png’)`,is this API just cheating thing? – Louise Jun 01 '16 at 10:15
  • 1
    See this selenium issue explaining that the only browser supporting this is the EdgeDriver currently: https://github.com/seleniumhq/selenium/issues/912 – Mobrockers Jun 01 '16 at 10:27
  • 1
    This method might not work as well as you expect due to image resizing issues while saving. – GiriB Apr 21 '17 at 18:45
4

The screenshot of a web element is not implemented in the Firefox driver. A workaround would be to crop the targeted element from the screenshot:

import StringIO
from selenium import webdriver
from PIL import Image

driver = webdriver.Firefox()
driver.get('http://stackoverflow.com')

# get the logo element
element = driver.find_element_by_id('hlogo')

# crop to the logo from the screenshot
rect = element.rect
points = [rect['x'], rect['y'], rect['x'] + rect['width'], rect['y'] + rect['height']]
with Image.open(StringIO.StringIO(driver.get_screenshot_as_png())) as img :
    with img.crop(points) as imgsub :
        imgsub.save("c:\\temp\\logo.png", 'PNG')
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • What browser does support it? I just tried it on Chrome and this doesn't work either. – RemcoW Jun 01 '16 at 10:21
  • The only driver currently supporting it is the EdgeDriver: https://github.com/seleniumhq/selenium/issues/912 – Mobrockers Jun 01 '16 at 10:27
  • I test 'webelement.screenshot_as_base64()' in phantomjs browser,this method cant work in phantomjs as well.so it seem not just the Firefox problem. – Louise Jun 01 '16 at 10:31