Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get the screenshot of the current element #2898

Closed
GoverSky opened this issue Oct 9, 2016 · 4 comments
Closed

Can't get the screenshot of the current element #2898

GoverSky opened this issue Oct 9, 2016 · 4 comments

Comments

@GoverSky
Copy link

GoverSky commented Oct 9, 2016

Meta -

OS: Windows 10

Selenium Version: 2.53.6

Browser: Chrome

Browser Version: 53.0.2785.116 m (64-bit)

Expected Behavior -

Gets the screenshot of the current element as a binary data.

Actual Behavior -

It raise WebDriverException

Traceback (most recent call last):
  File "C:\python2.7\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-6-73528134d358>", line 1, in <module>
    element.screenshot_as_png
  File "C:\python2.7\lib\site-packages\selenium\webdriver\remote\webelement.py", line 400, in screenshot_as_png
    return base64.b64decode(self.screenshot_as_base64.encode('ascii'))
  File "C:\python2.7\lib\site-packages\selenium\webdriver\remote\webelement.py", line 390, in screenshot_as_base64
    return self._execute(Command.ELEMENT_SCREENSHOT)['value']
  File "C:\python2.7\lib\site-packages\selenium\webdriver\remote\webelement.py", line 461, in _execute
    return self._parent.execute(command, params)
  File "C:\python2.7\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\python2.7\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 163, in check_response
    raise exception_class(value)
WebDriverException: Message: unknown command: session/b29be55e0ac21893f180da11ac203787/screenshot/0.20772185933294707-1

Steps to reproduce -

with python2.7

from selenium import webdriver
driver= webdriver.Chrome()
driver.get("http://www.google.com")
element = driver.find_element_by_id('lst-ib')
print element.screenshot_as_png
@lukeis
Copy link
Member

lukeis commented Oct 9, 2016

Please log issues for chromedriver with that project:
https://bugs.chromium.org/p/chromedriver/issues/list

Element screenshots are a new thing introduced in the w3c spec, but not widely implemented. I suggest logging a bug with the chromedriver project.

@lukeis lukeis closed this as completed Oct 9, 2016
@AdaNammaAyyasamy
Copy link

I get the same Issue. @GoverSky were you able to find any solution.

@GoverSky
Copy link
Author

@AdaNammaAyyasamy Yes,I've got a solution like this:

from selenium import webdriver
from PIL import Image
driver= webdriver.Chrome()
driver.get('https://www.python.org/')
element = driver.find_element_by_class_name('python-logo')
driver.get_screenshot_as_file('screenshot.png')
left = int(element.location['x']) 
top = int(element.location['y']) 
right = int(element.location['x'] + element.size['width']) 
bottom = int(element.location['y'] + element.size['height']) 
print (left, top, right, bottom)
im = Image.open('screenshot.png')
im = im.crop((left, top, right, bottom))
im.save('new_screenshot.png')

@cgoldberg
Copy link
Contributor

element.screenshot('/tmp/screenshot.png')

@lock lock bot locked and limited conversation to collaborators Aug 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants