I want to capture captcha code with python and selenium. So far I have this:
elem = driver.find_element_by_css_selector("#imagecpt")
loc, size = elem.location, elem.size
left, top = loc['x'], (loc['y'] - 587)
width, height = size['width'], size['height']
box = (int(left), int(top), int(left+width), int(top+height))
screenshot = driver.get_screenshot_as_base64()
img = Image.open(StringIO.StringIO(base64.b64decode(screenshot)))
captcha = img.crop(box)
captcha.save('captcha.png', 'PNG')
The code is working but there is a small problem, the top position returned by elem.location is complete position in website.
To capture the captcha code in my website I need to scroll down the page, as Selenium takes screenshot only for the visible part of the website and the top size returned by elem.location is not valid.
To solve the scrolling problem I am using a hardcoded value - 587. How can I rewrite the code without this hardcoded value?