I can screenshot a whole page using Firefox.get_screenshot_as_file(\'2.png\'),but when I screenshot a web element using passage.screenshot(\'1.png\'),it alway raise
You're getting this exception because you cannot take a screenshot of just an element in selenium without some third-party libraries or your own code to handle this. See This stackoverflow post
Which uses a library called PIL to do it:
from selenium import webdriver
from PIL import Image
fox = webdriver.Firefox()
fox.get('https://stackoverflow.com/')
# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
fox.save_screenshot('screenshot.png') # saves screenshot of entire page
fox.quit()
im = Image.open('screenshot.png') # uses PIL library to open image in memory
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image