如何从字符串生成带有selenium/phantomjs的png文件?

6 python selenium phantomjs

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

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"
Run Code Online (Sandbox Code Playgroud)

cod*_*iot 12

PhantomJS

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

这是如何在phantomJS中获取截图,我已经使用了phantomJS一段时间了.

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

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

希望这可以帮助.