Skip to content

Commit

Permalink
adding element screenshot to python
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeis committed Jun 25, 2015
1 parent cdbccbf commit 360cd32
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions py/CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Selenium 2.47.0 (in development)
* Adding ability to make remote call for webelement screenshots in accordance to the W3C spec

Selenium 2.46.0
* Firefox support up to 38
* BlackBerry browser support
Expand Down
1 change: 1 addition & 0 deletions py/selenium/webdriver/remote/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Command(object):
GET_ELEMENT_VALUE_OF_CSS_PROPERTY = "getElementValueOfCssProperty"
ELEMENT_EQUALS = "elementEquals"
SCREENSHOT = "screenshot"
ELEMENT_SCREENSHOT = "elementScreenshot"
IMPLICIT_WAIT = "implicitlyWait"
EXECUTE_ASYNC_SCRIPT = "executeAsyncScript"
SET_SCRIPT_TIMEOUT = "setScriptTimeout"
Expand Down
1 change: 1 addition & 0 deletions py/selenium/webdriver/remote/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def __init__(self, remote_server_addr, keep_alive=False):
Command.GET_TITLE: ('GET', '/session/$sessionId/title'),
Command.GET_PAGE_SOURCE: ('GET', '/session/$sessionId/source'),
Command.SCREENSHOT: ('GET', '/session/$sessionId/screenshot'),
Command.ELEMENT_SCREENSHOT: ('GET', '/session/$sessionId/screenshot/$id'),
Command.FIND_ELEMENT: ('POST', '/session/$sessionId/element'),
Command.FIND_ELEMENTS: ('POST', '/session/$sessionId/elements'),
Command.GET_ACTIVE_ELEMENT:
Expand Down
42 changes: 42 additions & 0 deletions py/selenium/webdriver/remote/webelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,48 @@ def rect(self):
"""A dictionary with the size and location of the element."""
return self._execute(Command.GET_ELEMENT_RECT)['value']

@property
def screenshot_as_base64(self):
"""
Gets the screenshot of the current element as a base64 encoded string.
:Usage:
img_b64 = element.screenshot_as_base64
"""
retrun self._exceute(Command.ELEMENT_SCREENSHOT)['value']

@property
def screenshot_as_png(self):
"""
Gets the screenshot of the current element as a binary data.
:Usage:
element_png = element.screenshot_as_png
"""
return base64.b64decode(self.screenshot_as_base64.encode('ascii'))

def screenshot(self, filename):
"""
Gets the screenshot of the current element. 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.
:Usage:
element.screenshot('/Screenshots/foo.png')
"""
png = self.screenshot_as_png
try:
with open(filename, 'wb') as f:
f.write(png)
except IOError:
return False
finally:
del png
return True


@property
def parent(self):
"""Internal reference to the WebDriver instance this element was found from."""
Expand Down

0 comments on commit 360cd32

Please sign in to comment.