Webdriver Screenshot

后端 未结 12 808
南方客
南方客 2020-11-30 00:57

When taking a screenshot using Selenium Webdriver on windows with python, the screenshot is saved directly to the path of the program, is there a way to save the .png file t

12条回答
  •  离开以前
    2020-11-30 01:46

    Here they asked a similar question, and the answer seems more complete, I leave the source:

    How to take partial screenshot with Selenium WebDriver in python?

    from selenium import webdriver
    from PIL import Image
    from io import BytesIO
    
    fox = webdriver.Firefox()
    fox.get('http://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
    png = fox.get_screenshot_as_png() # saves screenshot of entire page
    fox.quit()
    
    im = Image.open(BytesIO(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
    

提交回复
热议问题