Many times we want to have a screenshot of the screen/web page that we are working with. In order to do that, Selenium Python API provides us with the simplest of solution.
We can use the
save_screenshot( ) method to take a screenshot of the web page or url that we are working with right now.
Here is a working code for Firefox
__author__ = 'rahul' import unittest from selenium import webdriver class MyScreenshot(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() def test_takescreenshot(self): driver = self.driver driver.maximize_window() driver.get('http://www.espncricinfo.com/') driver.save_screenshot('/home/rahul/Downloads/firefoximage.png') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
Taking Screenshot on Chrome vs Taking Screenshot in Firefox
You will notice that there is a difference when it comes to taking screenshot between Firefox and Chrome. When you use the ChromeDriver to initiate a chrome browser and try taking a screenshot, it will only take screenshot of the visible window- that much area which is visible to a user on browser at a single glance
This screenshot is taken with a Chrome Driver, and so it only shows that area which is visible on browser to a user.
Firefox on the other hand behaves differently. FirefoxDriver takes the screenshot of the whole tab- up to the document end and so you have a different screenshot when you use Firefox browser.
This whole tab screenshot comes if you’re using FirefoxDriver. Always keep this thing in mind when using this function.
The difference occurs due to an issue with ChromeDriver, and Chromium. Here is the bug link that has been submitted and has not been fixed for the last two years. As per the new Webdriver specifications mentioned here, the screenshot method will only take the screenshot of the view port and that is why it looks like the Chrome driver screenshot issue won’t be resolved (ever).