其实也没有什么好解释的,原理就是在 截全屏图之后, 再次二度截取图片
找到元素定位的图片位置, 下面的代码复制粘贴即可
# from selenium import webdriver
# import time
# driver = webdriver.Chrome()
# driver.get('https://www.baidu.com/')
# time.sleep(3)
# driver.save_screenshot('screenshot.png')
# driver.quit()
# broswer.save_screenshot(r'photo.png')
# baidu = broswer.find_element_by_id('su')
# print(baidu)
# left = baidu.location['x']
# top = baidu.location['y']
# elementWidth = baidu.location['x'] + baidu.size['width']
# elementHeight = baidu.location['y'] + baidu.size['height']
# picture = Image.open(r'photo.png')
# picture = picture.crop((left, top, elementWidth, elementHeight))
# picture.save(r'photo2.png')
# broswer.quit()