Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium screenshot captcha image

i want to take screenshot and crop only the captcha image, im write this code : http://pastebin.com/Hqau6kRD :

elem = driver.find_element_by_css_selector("#imagecpt")
loc  = elem.location
size = elem.size
left  = loc['x']
top   = loc['y']
width = size['width']
height = size['height']
box = (int(left), int(top), int(left+width), int(top+height))
screenshot = driver.get_screenshot_as_png()
img = Image.open(StringIO.StringIO(screenshot))
area = img.crop(box)
area.save('screenshot.png', 'PNG')

the image saved is complete black, where im wrong ?

like image 747
kingcope Avatar asked May 25 '26 22:05

kingcope


1 Answers

Yeah. For Python3 it will be like:

...
from io import BytesIO
...
screenshot = driver.get_screenshot_as_base64()
img = Image.open(BytesIO(base64.b64decode(screenshot))
...
like image 65
Vitaliy Davydovych Avatar answered May 28 '26 14:05

Vitaliy Davydovych



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!