7

I want to screenshot an element in Selenium, according to the document, each WebElement has a function:

screenshot(filename)

Saves a screenshot of the current element to a PNG image file. Returns False if there is any IOError, else returns True. Use full paths in your filename.

Args: filename: The full path you wish to save your screenshot to. This should end with a .png extension

Usage: element.screenshot(‘/Screenshots/foo.png’)

However, when I use this function in my program:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep


url='http://www.google.com'
browser = webdriver.Chrome()  
browser.get(url)
content = browser.find_element_by_id('searchform')
content.screenshot('/home/ding/Pictures/shot.png')

It raise error like this:

Traceback (most recent call last):

  File "<ipython-input-8-309cb404878d>", line 11, in <module>
    content.screenshot('/home/ding/Pictures/shot.png')

  File "/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 453, in screenshot
    png = self.screenshot_as_png

  File "/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 435, in screenshot_as_png
    return base64.b64decode(self.screenshot_as_base64.encode('ascii'))

  File "/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 425, in screenshot_as_base64
    return self._execute(Command.ELEMENT_SCREENSHOT)['value']

  File "/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 501, in _execute
    return self._parent.execute(command, params)

  File "/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
    self.error_handler.check_response(response)

  File "/home/ding/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 165, in check_response
    raise exception_class(value)

WebDriverException: unknown command: session/efbca24571c5332230f4d032ae04787c/element/0.7487814861441955-1/screenshot

How can I solve this and take a screenshot of an element using Selenium in Python?

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
ding
  • 71
  • 1
  • 3
  • 4
    Apparently it doesnt work : https://github.com/seleniumhq/selenium/issues/912. But there are workarounds: https://stackoverflow.com/questions/15018372/how-to-take-partial-screenshot-with-selenium-webdriver-in-python – BoboDarph Nov 10 '17 at 14:13
  • Thank you, that's what I want. – ding Nov 12 '17 at 06:39

4 Answers4

4

This Code works perfectly for taking screenshot of a particular element in Firefox browser.

from selenium import webdriver
import io
from PIL import Image

fox = webdriver.Firefox()
fox.get('http://stackoverflow.com/')
image = fox.find_element_by_id('hlogo').screenshot_as_png
imageStream = io.BytesIO(image)
im = Image.open(imageStream)
im.save(image_path)
  • Please refer to this thread for Chrome browser options. https://stackoverflow.com/questions/15018372/how-to-take-partial-screenshot-with-selenium-webdriver-in-python – Sudharsana Rajasekaran Sep 25 '19 at 20:57
2

Importing additional libraries to write a file seems silly to me...

Use the built-in open:

button_element = self._driver.find_element_by_class_name("a-button-input")
with open("element.png", "wb") as elem_file:
    elem_file.write(button_element.screenshot_as_png)

Amazon Element Output:

enter image description here

Eric Fossum
  • 2,395
  • 4
  • 26
  • 50
0

You need to replace the screenshot with save_screenshot method. So

Instead of :

content.screenshot('/home/ding/Pictures/shot.png')

Use :

content.save_screenshot('/home/ding/Pictures/shot.png')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep


url="http://www.google.com"
browser = webdriver.Chrome("c:\chrome\chromedriver.exe")
browser.get(url)
browser.find_element_by_id('searchform')
browser.save_screenshot("C://Demo//shot.png")
gole
  • 7
  • 1