如何从字符串生成带有selenium/phantomjs的png文件? [英] How do I generate a png file w/ selenium/phantomjs from a string?

查看:57
本文介绍了如何从字符串生成带有selenium/phantomjs的png文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用selenium/phantomjs在python中创建html的png文件.有没有办法从html字符串或文件句柄(而不是网站)生成png?我已经搜索了硒文档并用谷歌搜索,但是找不到答案.我有:

I am using selenium/phantomjs to create png files of html in python. Is there a way to generate the png from an html string or filehandle (instead of a website)? I've searched through the selenium docs and googled but couldn't find an answer. I have:

htmlString = '<html><body><div style="background-color:red;height:500px;width:500px;">This is a png</div></body></html>'
myFile = 'tmp.html'
f = open(myFile,'w')
f.write(htmlString) 

from selenium import webdriver  

driver = webdriver.PhantomJS()
driver.set_window_size(1024, 768) 
#driver.get('https://google.com/') # this works fine
driver.get(myFile) # passing the file name or htmlString doesn't work...creates a blank png with nothing
driver.save_screenshot('screen.png') 
driver.quit()

print "png file created"

推荐答案

PhantomJS

var page = require('webpage').create();
page.open('http://github.com/', function () {
    page.render('github.png');
    phantom.exit();
});

这是在phantomJS中获取屏幕截图的方法,我已经使用phantomJS已有一段时间了.

This is how to get a screenshot in phantomJS, I've used phantomJS for some time now.

您可以在此处找到更多信息.

driver = webdriver.Chrome();
driver.get('http://www.google.com');
driver.save_screenshot('out.png');
driver.quit();

希望这会有所帮助.

这篇关于如何从字符串生成带有selenium/phantomjs的png文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆