大概率是因为电脑显示,设置了dpi缩放,比如放大了1.25倍 那么截图时,把坐标和宽度高度,都放大即可。
from PIL import Image ... d.save_screenshot('xx.png') element = d.find_element_by_xpath("//div[contains(@class,'shumei_captcha_wrapper')]") print(element.location) # 打印元素坐标 print(element.size) # 打印元素大小
left = element.location['x']*1.25 top = element.location['y']*1.25 right = element.location['x']*1.25 + element.size['width']*1.25 bottom = element.location['y']*1.25 + element.size['height']*1.25
im = Image.open('xx.png') im = im.crop((left, top, right, bottom)) im.save('yy.png')
|