#!/usr/bin/env python # encoding: utf-8 import time from selenium import webdriver from PIL import Image from io import BytesIO driver=webdriver.Chrome() driver.get("http://www.baidu.com/") img=driver.find_element_by_xpath("//div/img") location=img.location size=img.size x=location["x"] y=location["y"] height=size["height"] wid=size["width"] im=Image.open(BytesIO(driver.get_screenshot_as_png())) im.show() box=(x,y,x+wid,y+height) #这四个参数表示要截取的坐标 左上角的x,y右下角的x,y captcha=im.crop(box) #截取上面指定的区域 captcha.show()