Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to screenshot a specified WebElement in Selenium using Python

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?

like image 556
ding Avatar asked Nov 10 '17 13:11

ding


People also ask

How do you take a screenshot of a specific area in Selenium Python?

Take the first screenshot using save_screenshot In this example, Selenium will navigate to a specific URL (using the Chrome browser) and then take a screenshot using the save_screenshot() function.

How do you take a screenshot of a specific element in Python?

For capturing the screenshot, save_screenshot() method is available. This method takes the full page screenshot. There is no in built method to capture an element. To achieve this we have to crop the image of the full page to the particular size of the element.

Can we take screenshot of single Webelement in Selenium?

We can capture screenshots of a particular element in Selenium 4.0 with the help of getScreenshotAs(OutputType. File) method where the OutputType tells about the output type of the screenshot.


1 Answers

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)
like image 52
Sudharsana Rajasekaran Avatar answered Oct 09 '22 17:10

Sudharsana Rajasekaran