is it possible to render a webpage directly to an image in python?
Asked Answered
W

3

10

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?

Wavelength answered 27/11, 2013 at 19:45 Comment(8)
if you use Linux/Unix and you're brave, you could experiment with running the web browser with Xvfb, an X11 server which renders images in memory without displaying them.Fidele
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.Topcoat
another option is to use Qt webkit bindings: linux.com/learn/docs/ldp/…Shifflett
Thanks All, I found a solution as @AndreHolzner mentioned, #6183776Wavelength
@Wavelength did it actually work as expected? Please let us know.Algophobia
@Algophobia , it save the image and not popup the window :)Wavelength
@Wavelength 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.Algophobia
You should consider answering your own question, now that you have found a solution.Rag
W
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()
Wavelength answered 11/1, 2014 at 15:45 Comment(0)
F
1

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

Fite answered 11/1, 2014 at 15:58 Comment(0)
B
0

Selenium can now do this without any other libraries by using the headless option

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
browser = webdriver.Firefox(options=options)
browser.set_window_size(1600,800)
browser.get('https://www.google.com')
browser.save_full_page_screenshot('./out.png')

browser.close()

Becquerel answered 22/10, 2022 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.