Skip to content

Instantly share code, notes, and snippets.

@gustavi
Last active July 12, 2017 13:53
Show Gist options
  • Save gustavi/e509b36afbf1a477d3ef37daf075b557 to your computer and use it in GitHub Desktop.
Save gustavi/e509b36afbf1a477d3ef37daf075b557 to your computer and use it in GitHub Desktop.
Python Patterns
def get_first_day_of_week(day):
"""
Get the first day of week of a datetime (similar with a date).
""""
day.replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=day.weekday())
def take_screenshots_of_element(element):
"""
Take screenshot of an Selenium element (PIL/Pillow required).
:param element: Selenium WebElement
"""
img_name = 'element.png'
loc_x = element.location.get('x')
loc_y = element.location.get('y')
width = element.size.get('width')
height = element.size.get('height')
driver.get_screenshot_as_file(img_name)
img = Image.open(img_name)
img = img.crop((loc_x, loc_y, loc_x+width, loc_y+height))
img.save(img_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment