Search code examples
pythonbrowserscreenshotscreensaverwxwidgets

Take a screenshot of open website in python script


I need to write a python script which opens a website and when the website is completly opened it takes a screenshot of the opened website.

I wrote sth like this:

import webbrowser
import wx

wx.App()
link = "http://stackoverflow.com/questions"
webbrowser.get('firefox %s').open_new_tab(link)
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.EmptyBitmap(size[0], size[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, size[0], size[1], screen, 0, 0)
del mem
bmp.SaveFile('screenshot.png', wx.BITMAP_TYPE_PNG)

It only opens a new tab in firefox but it doesnt take a screenshot of it :(

I want the solution to be cross-platform. Thanks for any help:)

EDIT:

The main problem here is that the script musnt take a picture BEFORE my webpage is completly opened. How to solve that issue?


Solution

  • This solution is fairly cross-platform, but if you're trying to show a bit of a web page open in a desktop with menu/toolbars etc... it's not what you want.

    You could use SeleniumHQ to automate a browser of your choice, have that browser render the page, then get the complete image of the rendered page - ie, not just a screenshot of the portion of the page that's displayed on screen. You could then crop that accordingly.