之所以会出现此异常,是因为如果没有某些第三方库或您自己的代码来处理硒中的元素,就无法截取它的屏幕快照。
它使用称为PIL的库来执行此操作:
from selenium import webdriverfrom PIL import Imagefox = webdriver.Firefox()fox.get('https://http://codingdict.com/')# now that we have the preliminary stuff out of the way time to get that image :Delement = fox.find_element_by_id('hlogo') # find part of the page you want image oflocation = element.locationsize = element.sizefox.save_screenshot('screenshot.png') # saves screenshot of entire pagefox.quit()im = Image.open('screenshot.png') # uses PIL library to open image in memoryleft = location['x']top = location['y']right = location['x'] + size['width']bottom = location['y'] + size['height']im = im.crop((left, top, right, bottom)) # defines crop pointsim.save('screenshot.png') # saves new cropped image


