【问题标题】:How do I generate a png file w/ selenium/phantomjs from a string?如何从字符串生成带有 selenium/phantomjs 的 png 文件?
【发布时间】: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:/// 是浏览器用来访问文件的。我没有转义上面的行(现在我无法编辑它)...你确定它转义了吗?

标签: python selenium phantomjs


【解决方案1】:

PhantomJS

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

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

您可以找到更多信息here.

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

希望这会有所帮助。

【讨论】:

  • 您应该从 Selenium (Python) 部分中删除分号。
  • 你不必
【解决方案2】:

纯好的旧 python - 通过 JS 将任何打开的页面上的内容设置为目标 html。以您的示例代码为例:

from selenium import webdriver

htmlString = '<html><body><div style="background-color:red;height:500px;width:500px;">This is a png</div></body></html>'

driver = webdriver.PhantomJS() # the normal SE phantomjs binding
driver.set_window_size(1024, 768)
driver.get('https://google.com/') # whatever reachable url
driver.execute_script("document.write('{}');".format(htmlString))  # changing the DOM

driver.save_screenshot('screen.png')   #screen.png is a big red rectangle :)
driver.quit()

print "png file created"

【讨论】:

    【解决方案3】:

    好像是线条

    f = open(myFile,'w')
    f.write(htmlString) 
    

    有问题,因为生成的文件是空的。

    我用

    解决了这个问题
    with open(myFile,'wt') as f:
        f.write(htmlString)
    

    或者你可能需要添加一个

    f.close() to your code
    

    【讨论】:

      【解决方案4】:

      PhantomJS

      var page = require('webpage').create();
      page.content = '<html><body><p>Hello world</p></body></html>';
      page.render('name.png');
      

      您使用page.content 设置页面内容 然后你使用page.render渲染它

      Example using phantomjs-node

      phantom.create(function (ph) {
        ph.createPage(function (page) {
            page.set('viewportSize', {width:1440,height:900})
      
            //like this
            page.set('content', html);
      
            page.render(path_to_pdf, function() { 
              //now pdf is written to disk.
              ph.exit();
            });
        });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-14
        • 2019-06-18
        • 2010-10-11
        • 1970-01-01
        • 2016-11-18
        • 2018-02-28
        • 1970-01-01
        相关资源
        最近更新 更多