python selenium screenshot captcha image

时间:2015-12-30 13:21:46

标签: python selenium

我想拍摄截图并仅裁剪验证码图片,即时编写此代码:enter image description here

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')

保存的图片是完整的黑色,我错了吗?

2 个答案:

答案 0 :(得分:0)

我假设您正在使用PIL库中的Image模块。

尝试获取base64值,对其进行解码,然后将其作为参数传递给open()方法。

...
screenshot = driver.get_screenshot_as_base64()
img = Image.open(StringIO.StringIO(base64.b64decode(screenshot))
...

答案 1 :(得分:0)

呀。对于Python3,它将像:

{{1}}
相关问题