【发布时间】:2013-08-05 20:21:56
【问题描述】:
我正在使用 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"
【问题讨论】:
-
你试过
file:///PathToFile/tmp.html吗? -
这也给了我一个空白的 png 文件。这在任何地方都有记录吗?
-
没有。我从来没有使用过 PhantomJS,但是 file:/// 是浏览器用来访问文件的。我没有转义上面的行(现在我无法编辑它)...你确定它转义了吗?