I'm attempting to take a screenshot of a specific element on a webpage using the code in the answer to this question. This initially worked when I tested it a few days ago, but now the area it yields is consistently up and to the left of the target element.
The output of the original code isn't very helpful in debugging, so I've altered it to draw a rectangle around the area instead of cropping it.
Example:
from selenium import webdriver
from PIL import Image, ImageDraw
from io import BytesIO
browser = webdriver.Chrome()
browser.get('http://www.google.com')
logo = browser.find_element_by_id('hplogo') #id of 'Google' image
location = logo.location
size = logo.size
im = Image.open(BytesIO(browser.get_screenshot_as_png()))
draw = ImageDraw.Draw(im)
draw.rectangle(((location['x'], location['y']), (location['x'] + size['width'], location['y'] + size['height'])), outline='black')
im.show()
browser.quit()
The box drawn seems to be in the right aspect ratio, but incorrect location and size. I would appreciate any explanation for what's causing this issue and any help with fixing it.