Webdriver Screenshot












45















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 to a specific directory?










share|improve this question





























    45















    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 to a specific directory?










    share|improve this question



























      45












      45








      45


      11






      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 to a specific directory?










      share|improve this question
















      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 to a specific directory?







      python selenium selenium-webdriver webdriver






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '16 at 15:17









      Danra

      5,09814294




      5,09814294










      asked Jan 17 '12 at 18:55









      user1152578user1152578

      257158




      257158
























          9 Answers
          9






          active

          oldest

          votes


















          63














          Use driver.save_screenshot('/path/to/file') or driver.get_screenshot_as_file('/path/to/file'):



          import selenium.webdriver as webdriver
          import contextlib

          @contextlib.contextmanager
          def quitting(thing):
          yield thing
          thing.quit()

          with quitting(webdriver.Firefox()) as driver:
          driver.implicitly_wait(10)
          driver.get('http://www.google.com')
          driver.get_screenshot_as_file('/tmp/google.png')
          # driver.save_screenshot('/tmp/google.png')





          share|improve this answer





















          • 1





            Hi, driver.save_screenshot('/path/to/file') works on Windows, but driver.get_screenshot_as_file('/path/to/file') doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates <div class="google-recaptcha"> or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".

            – Tommy L
            Apr 20 '18 at 16:10













          • @TommyL: save_screenshot calls get_screenshot_as_file, so if one worked, so should the other.

            – unutbu
            Apr 20 '18 at 20:30











          • @TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.

            – unutbu
            Apr 20 '18 at 20:36



















          23














          Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver



          from selenium import webdriver

          browser = webdriver.Firefox()
          browser.get('http://www.google.com/')
          browser.save_screenshot('screenie.png')
          browser.quit()





          share|improve this answer

































            8














            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('Dfolderfilename.png')





            share|improve this answer

































              3














              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"[^ws]", '', 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'





              share|improve this answer

































                3














                This will take screenshot and place it in a directory of a chosen name.



                import os
                driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))





                share|improve this answer


























                • It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.

                  – sniperd
                  Jun 27 '18 at 15:15



















                2














                driver.save_screenshot("path to save \screen.jpeg")





                share|improve this answer































                  2














                  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






                  share|improve this answer

































                    2














                    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





                    share|improve this answer































                      -7














                      I understand you are looking for an answer in python, but here is how one would do it in ruby..



                      http://watirwebdriver.com/screenshots/



                      If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.



                      eg:



                       image = b.screenshot.png

                      File.open("testfile.png", "w") do |file|
                      file.puts "#{image}"
                      end


                      where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.






                      share|improve this answer























                        Your Answer






                        StackExchange.ifUsing("editor", function () {
                        StackExchange.using("externalEditor", function () {
                        StackExchange.using("snippets", function () {
                        StackExchange.snippets.init();
                        });
                        });
                        }, "code-snippets");

                        StackExchange.ready(function() {
                        var channelOptions = {
                        tags: "".split(" "),
                        id: "1"
                        };
                        initTagRenderer("".split(" "), "".split(" "), channelOptions);

                        StackExchange.using("externalEditor", function() {
                        // Have to fire editor after snippets, if snippets enabled
                        if (StackExchange.settings.snippets.snippetsEnabled) {
                        StackExchange.using("snippets", function() {
                        createEditor();
                        });
                        }
                        else {
                        createEditor();
                        }
                        });

                        function createEditor() {
                        StackExchange.prepareEditor({
                        heartbeatType: 'answer',
                        autoActivateHeartbeat: false,
                        convertImagesToLinks: true,
                        noModals: true,
                        showLowRepImageUploadWarning: true,
                        reputationToPostImages: 10,
                        bindNavPrevention: true,
                        postfix: "",
                        imageUploader: {
                        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                        allowUrls: true
                        },
                        onDemand: true,
                        discardSelector: ".discard-answer"
                        ,immediatelyShowMarkdownHelp:true
                        });


                        }
                        });














                        draft saved

                        draft discarded


















                        StackExchange.ready(
                        function () {
                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8900073%2fwebdriver-screenshot%23new-answer', 'question_page');
                        }
                        );

                        Post as a guest















                        Required, but never shown

























                        9 Answers
                        9






                        active

                        oldest

                        votes








                        9 Answers
                        9






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes









                        63














                        Use driver.save_screenshot('/path/to/file') or driver.get_screenshot_as_file('/path/to/file'):



                        import selenium.webdriver as webdriver
                        import contextlib

                        @contextlib.contextmanager
                        def quitting(thing):
                        yield thing
                        thing.quit()

                        with quitting(webdriver.Firefox()) as driver:
                        driver.implicitly_wait(10)
                        driver.get('http://www.google.com')
                        driver.get_screenshot_as_file('/tmp/google.png')
                        # driver.save_screenshot('/tmp/google.png')





                        share|improve this answer





















                        • 1





                          Hi, driver.save_screenshot('/path/to/file') works on Windows, but driver.get_screenshot_as_file('/path/to/file') doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates <div class="google-recaptcha"> or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".

                          – Tommy L
                          Apr 20 '18 at 16:10













                        • @TommyL: save_screenshot calls get_screenshot_as_file, so if one worked, so should the other.

                          – unutbu
                          Apr 20 '18 at 20:30











                        • @TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.

                          – unutbu
                          Apr 20 '18 at 20:36
















                        63














                        Use driver.save_screenshot('/path/to/file') or driver.get_screenshot_as_file('/path/to/file'):



                        import selenium.webdriver as webdriver
                        import contextlib

                        @contextlib.contextmanager
                        def quitting(thing):
                        yield thing
                        thing.quit()

                        with quitting(webdriver.Firefox()) as driver:
                        driver.implicitly_wait(10)
                        driver.get('http://www.google.com')
                        driver.get_screenshot_as_file('/tmp/google.png')
                        # driver.save_screenshot('/tmp/google.png')





                        share|improve this answer





















                        • 1





                          Hi, driver.save_screenshot('/path/to/file') works on Windows, but driver.get_screenshot_as_file('/path/to/file') doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates <div class="google-recaptcha"> or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".

                          – Tommy L
                          Apr 20 '18 at 16:10













                        • @TommyL: save_screenshot calls get_screenshot_as_file, so if one worked, so should the other.

                          – unutbu
                          Apr 20 '18 at 20:30











                        • @TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.

                          – unutbu
                          Apr 20 '18 at 20:36














                        63












                        63








                        63







                        Use driver.save_screenshot('/path/to/file') or driver.get_screenshot_as_file('/path/to/file'):



                        import selenium.webdriver as webdriver
                        import contextlib

                        @contextlib.contextmanager
                        def quitting(thing):
                        yield thing
                        thing.quit()

                        with quitting(webdriver.Firefox()) as driver:
                        driver.implicitly_wait(10)
                        driver.get('http://www.google.com')
                        driver.get_screenshot_as_file('/tmp/google.png')
                        # driver.save_screenshot('/tmp/google.png')





                        share|improve this answer















                        Use driver.save_screenshot('/path/to/file') or driver.get_screenshot_as_file('/path/to/file'):



                        import selenium.webdriver as webdriver
                        import contextlib

                        @contextlib.contextmanager
                        def quitting(thing):
                        yield thing
                        thing.quit()

                        with quitting(webdriver.Firefox()) as driver:
                        driver.implicitly_wait(10)
                        driver.get('http://www.google.com')
                        driver.get_screenshot_as_file('/tmp/google.png')
                        # driver.save_screenshot('/tmp/google.png')






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 4 '14 at 0:43

























                        answered Jan 17 '12 at 19:03









                        unutbuunutbu

                        560k10512051258




                        560k10512051258








                        • 1





                          Hi, driver.save_screenshot('/path/to/file') works on Windows, but driver.get_screenshot_as_file('/path/to/file') doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates <div class="google-recaptcha"> or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".

                          – Tommy L
                          Apr 20 '18 at 16:10













                        • @TommyL: save_screenshot calls get_screenshot_as_file, so if one worked, so should the other.

                          – unutbu
                          Apr 20 '18 at 20:30











                        • @TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.

                          – unutbu
                          Apr 20 '18 at 20:36














                        • 1





                          Hi, driver.save_screenshot('/path/to/file') works on Windows, but driver.get_screenshot_as_file('/path/to/file') doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates <div class="google-recaptcha"> or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".

                          – Tommy L
                          Apr 20 '18 at 16:10













                        • @TommyL: save_screenshot calls get_screenshot_as_file, so if one worked, so should the other.

                          – unutbu
                          Apr 20 '18 at 20:30











                        • @TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.

                          – unutbu
                          Apr 20 '18 at 20:36








                        1




                        1





                        Hi, driver.save_screenshot('/path/to/file') works on Windows, but driver.get_screenshot_as_file('/path/to/file') doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates <div class="google-recaptcha"> or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".

                        – Tommy L
                        Apr 20 '18 at 16:10







                        Hi, driver.save_screenshot('/path/to/file') works on Windows, but driver.get_screenshot_as_file('/path/to/file') doesn't. (Yes, I changed for "\"). But it helped, thank you. Have you got any idea guys how to check google's ReCaptcha with selenium? You will not be able to select any elements, even if HTML generates <div class="google-recaptcha"> or whatever... JS Scripts does not work too. For clarification, I do not mean solving images in reCaptcha, but only checking a checkbox "Im not a robot".

                        – Tommy L
                        Apr 20 '18 at 16:10















                        @TommyL: save_screenshot calls get_screenshot_as_file, so if one worked, so should the other.

                        – unutbu
                        Apr 20 '18 at 20:30





                        @TommyL: save_screenshot calls get_screenshot_as_file, so if one worked, so should the other.

                        – unutbu
                        Apr 20 '18 at 20:30













                        @TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.

                        – unutbu
                        Apr 20 '18 at 20:36





                        @TommyL: Regarding recaptcha -- try googling something like "selenium click google recaptcha". There are a number of potential leads, such as this one. If that doesn't work for you, you could consider posting a new question -- including your code so we understand what you've tried and what went wrong.

                        – unutbu
                        Apr 20 '18 at 20:36













                        23














                        Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver



                        from selenium import webdriver

                        browser = webdriver.Firefox()
                        browser.get('http://www.google.com/')
                        browser.save_screenshot('screenie.png')
                        browser.quit()





                        share|improve this answer






























                          23














                          Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver



                          from selenium import webdriver

                          browser = webdriver.Firefox()
                          browser.get('http://www.google.com/')
                          browser.save_screenshot('screenie.png')
                          browser.quit()





                          share|improve this answer




























                            23












                            23








                            23







                            Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver



                            from selenium import webdriver

                            browser = webdriver.Firefox()
                            browser.get('http://www.google.com/')
                            browser.save_screenshot('screenie.png')
                            browser.quit()





                            share|improve this answer















                            Inspired from this thread (same question for Java): Take a screenshot with Selenium WebDriver



                            from selenium import webdriver

                            browser = webdriver.Firefox()
                            browser.get('http://www.google.com/')
                            browser.save_screenshot('screenie.png')
                            browser.quit()






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited May 23 '17 at 12:10









                            Community

                            11




                            11










                            answered Dec 16 '13 at 12:23









                            Ran AdlerRan Adler

                            2,2961821




                            2,2961821























                                8














                                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('Dfolderfilename.png')





                                share|improve this answer






























                                  8














                                  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('Dfolderfilename.png')





                                  share|improve this answer




























                                    8












                                    8








                                    8







                                    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('Dfolderfilename.png')





                                    share|improve this answer















                                    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('Dfolderfilename.png')






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Dec 13 '12 at 8:49









                                    Sirko

                                    54.8k15102145




                                    54.8k15102145










                                    answered Dec 13 '12 at 8:31









                                    Kv.senthilkumarKv.senthilkumar

                                    68621427




                                    68621427























                                        3














                                        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"[^ws]", '', 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'





                                        share|improve this answer






























                                          3














                                          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"[^ws]", '', 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'





                                          share|improve this answer




























                                            3












                                            3








                                            3







                                            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"[^ws]", '', 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'





                                            share|improve this answer















                                            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"[^ws]", '', 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'






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited May 23 '17 at 12:10









                                            Community

                                            11




                                            11










                                            answered May 6 '15 at 10:23









                                            Vladimir KolenovVladimir Kolenov

                                            959




                                            959























                                                3














                                                This will take screenshot and place it in a directory of a chosen name.



                                                import os
                                                driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))





                                                share|improve this answer


























                                                • It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.

                                                  – sniperd
                                                  Jun 27 '18 at 15:15
















                                                3














                                                This will take screenshot and place it in a directory of a chosen name.



                                                import os
                                                driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))





                                                share|improve this answer


























                                                • It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.

                                                  – sniperd
                                                  Jun 27 '18 at 15:15














                                                3












                                                3








                                                3







                                                This will take screenshot and place it in a directory of a chosen name.



                                                import os
                                                driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))





                                                share|improve this answer















                                                This will take screenshot and place it in a directory of a chosen name.



                                                import os
                                                driver.save_screenshot(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'NameOfScreenShotDirectory', 'PutFileNameHere'))






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 26 '18 at 4:53









                                                hellpanderr

                                                2,53221633




                                                2,53221633










                                                answered Jun 27 '18 at 14:35









                                                Ger McGer Mc

                                                345418




                                                345418













                                                • It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.

                                                  – sniperd
                                                  Jun 27 '18 at 15:15



















                                                • It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.

                                                  – sniperd
                                                  Jun 27 '18 at 15:15

















                                                It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.

                                                – sniperd
                                                Jun 27 '18 at 15:15





                                                It's great to have a nicely formatted code for the answer but it's usually best practice to include a little explanation with it.

                                                – sniperd
                                                Jun 27 '18 at 15:15











                                                2














                                                driver.save_screenshot("path to save \screen.jpeg")





                                                share|improve this answer




























                                                  2














                                                  driver.save_screenshot("path to save \screen.jpeg")





                                                  share|improve this answer


























                                                    2












                                                    2








                                                    2







                                                    driver.save_screenshot("path to save \screen.jpeg")





                                                    share|improve this answer













                                                    driver.save_screenshot("path to save \screen.jpeg")






                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered May 8 '15 at 7:44









                                                    ShaikShaik

                                                    1888




                                                    1888























                                                        2














                                                        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






                                                        share|improve this answer






























                                                          2














                                                          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






                                                          share|improve this answer




























                                                            2












                                                            2








                                                            2







                                                            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






                                                            share|improve this answer















                                                            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







                                                            share|improve this answer














                                                            share|improve this answer



                                                            share|improve this answer








                                                            edited Jan 29 '18 at 12:54

























                                                            answered Jan 29 '18 at 7:32









                                                            Shubham JainShubham Jain

                                                            7,92353668




                                                            7,92353668























                                                                2














                                                                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





                                                                share|improve this answer




























                                                                  2














                                                                  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





                                                                  share|improve this answer


























                                                                    2












                                                                    2








                                                                    2







                                                                    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





                                                                    share|improve this answer













                                                                    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






                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Jun 7 '18 at 20:50









                                                                    gabriel de la cruzgabriel de la cruz

                                                                    211




                                                                    211























                                                                        -7














                                                                        I understand you are looking for an answer in python, but here is how one would do it in ruby..



                                                                        http://watirwebdriver.com/screenshots/



                                                                        If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.



                                                                        eg:



                                                                         image = b.screenshot.png

                                                                        File.open("testfile.png", "w") do |file|
                                                                        file.puts "#{image}"
                                                                        end


                                                                        where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.






                                                                        share|improve this answer




























                                                                          -7














                                                                          I understand you are looking for an answer in python, but here is how one would do it in ruby..



                                                                          http://watirwebdriver.com/screenshots/



                                                                          If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.



                                                                          eg:



                                                                           image = b.screenshot.png

                                                                          File.open("testfile.png", "w") do |file|
                                                                          file.puts "#{image}"
                                                                          end


                                                                          where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.






                                                                          share|improve this answer


























                                                                            -7












                                                                            -7








                                                                            -7







                                                                            I understand you are looking for an answer in python, but here is how one would do it in ruby..



                                                                            http://watirwebdriver.com/screenshots/



                                                                            If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.



                                                                            eg:



                                                                             image = b.screenshot.png

                                                                            File.open("testfile.png", "w") do |file|
                                                                            file.puts "#{image}"
                                                                            end


                                                                            where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.






                                                                            share|improve this answer













                                                                            I understand you are looking for an answer in python, but here is how one would do it in ruby..



                                                                            http://watirwebdriver.com/screenshots/



                                                                            If that only works by saving in current directory only.. I would first assign the image to a variable and then save that variable to disk as a PNG file.



                                                                            eg:



                                                                             image = b.screenshot.png

                                                                            File.open("testfile.png", "w") do |file|
                                                                            file.puts "#{image}"
                                                                            end


                                                                            where b is the browser variable used by webdriver. i have the flexibility to provide an absolute or relative path in "File.open" so I can save the image anywhere.







                                                                            share|improve this answer












                                                                            share|improve this answer



                                                                            share|improve this answer










                                                                            answered Oct 12 '12 at 6:57









                                                                            sambeherasambehera

                                                                            60121129




                                                                            60121129






























                                                                                draft saved

                                                                                draft discarded




















































                                                                                Thanks for contributing an answer to Stack Overflow!


                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                But avoid



                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                To learn more, see our tips on writing great answers.




                                                                                draft saved


                                                                                draft discarded














                                                                                StackExchange.ready(
                                                                                function () {
                                                                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8900073%2fwebdriver-screenshot%23new-answer', 'question_page');
                                                                                }
                                                                                );

                                                                                Post as a guest















                                                                                Required, but never shown





















































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown

































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown







                                                                                Popular posts from this blog

                                                                                Wiesbaden

                                                                                Marschland

                                                                                Dieringhausen