Webdriver Screenshot

后端 未结 12 766
南方客
南方客 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:36
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com/");
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File("c:\\NewFolder\\screenshot1.jpg"));
    
    0 讨论(0)
  • 2020-11-30 01:37
    driver.save_screenshot("path to save \\screen.jpeg")
    
    0 讨论(0)
  • 2020-11-30 01:42

    Yes, we have a way to get screenshot extension of .png using python webdriver

    use below code if you working in python webriver.it is very simple.

    driver.save_screenshot('D\folder\filename.png')
    
    0 讨论(0)
  • 2020-11-30 01:43

    Sure it isn't actual right now but I faced this issue also and my way: Looks like 'save_screenshot' have some troubles with creating files with space in name same time as I added randomization to filenames for escaping override.

    Here I got method to clean my filename of whitespaces (How do I replace whitespaces with underscore and vice versa?):

    def urlify(self, s):
        # Remove all non-word characters (everything except numbers and letters)
        s = re.sub(r"[^\w\s]", '', s)
        # Replace all runs of whitespace with a single dash
        s = re.sub(r"\s+", '-', s)
        return s
    

    then

    driver.save_screenshot('c:\\pytest_screenshots\\%s' % screen_name)
    

    where

    def datetime_now(prefix):
        symbols = str(datetime.datetime.now())
        return prefix + "-" + "".join(symbols)
    
    screen_name = self.urlify(datetime_now('screen')) + '.png'
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-30 01:48

    You can use below function for relative path as absolute path is not a good idea to add in script

    Import

    import sys, os
    

    Use code as below :

    ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
    screenshotpath = os.path.join(os.path.sep, ROOT_DIR,'Screenshots'+ os.sep)
    driver.get_screenshot_as_file(screenshotpath+"testPngFunction.png")
    

    make sure you create the folder where the .py file is present.

    os.path.join also prevent you to run your script in cross-platform like: UNIX and windows. It will generate path separator as per OS at runtime. os.sep is similar like File.separtor in java

    0 讨论(0)
提交回复
热议问题