我使用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"
幻影
这是如何在phantomJS中获得屏幕截图的,我已经使用phantomJS一段时间了。
您可以找到更多信息here.
硒
希望这有帮助。
幻影
使用
page.content
设置页面内容 然后使用page.render
呈现它Example using phantomjs-node
纯好的旧python-通过JS将任何打开页面上的内容设置为目标html。以您的示例代码为例:
相关问题 更多 >
编程相关推荐