我正在使用 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"