9

Now I am using selenium to save a web page to image.

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("some url")
browser.save_screenshot(img)
browser.quit()

But there is a problem that each time it will popup a window.

Is there any way that can render a image directly to an image?

camino
  • 9,449
  • 19
  • 60
  • 105
  • 2
    if you use Linux/Unix and you're brave, you could experiment with running the web browser with [Xvfb](http://en.wikipedia.org/wiki/Xvfb), an X11 server which renders images in memory without displaying them. – Andre Holzner Nov 27 '13 at 19:54
  • Firefox's rendering engine Gecko can actually be used for "off-screen" rendering to a buffer. But trying to interface with Gecko from Python—especially since they stopped supporting Gecko embedding after 5.0, and never finished the samples—is a nightmare. You _could_ do it by writing a XUL/JS app, running _that_ in Firefox/XULRunner, then scripting that with Python. But it's still not going to be fun. – abarnert Nov 27 '13 at 19:56
  • 1
    another option is to use Qt webkit bindings: http://www.linux.com/learn/docs/ldp/284676-converting-html-to-pdf-using-python-and-qt – georg Nov 27 '13 at 19:57
  • 2
    Thanks All, I found a solution as @AndreHolzner mentioned, http://stackoverflow.com/questions/6183276/how-do-i-run-selenium-in-xvfb – camino Nov 27 '13 at 20:02
  • @camino did it actually work as expected? Please let us know. – rdodev Nov 27 '13 at 20:05
  • @rdodev , it save the image and not popup the window :) – camino Nov 27 '13 at 20:06
  • @camino good news, I guess. When we tried a while back we never got a good solution working, then again, it was on Windows machines, which probably make a difference. – rdodev Nov 27 '13 at 20:08
  • You should consider answering your own question, now that you have found a solution. – daviewales Jan 11 '14 at 05:26

2 Answers2

3

I found a workaround at How do I run Selenium in Xvfb?

It works fine in linux.

from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get("some url")
browser.save_screenshot(img)
browser.quit()
display.stop()
Community
  • 1
  • 1
camino
  • 9,449
  • 19
  • 60
  • 105
2

You can use phantomjs

Here you can find a good tutorial: http://www.realpython.com/blog/python/headless-selenium-testing-with-python-and-phantomjs/#.UtFpiXMuKAg

Andrea de Marco
  • 819
  • 5
  • 9