2013-08-05 10 views
6

Python'da html png dosyaları oluşturmak için selenium/phantomjs kullanıyorum. Bir html dizesi veya filehandle (bir web sitesi yerine) png oluşturmanın bir yolu var mı? Selenyum dokümanlarını araştırdım ve googled ama bir cevap bulamadım. Ben:Bir dizeden png dosyası w/selenium/phantomjs nasıl oluştururum?

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" 
+0

Eğer 'dosyayı denediniz eklemek için

with open(myFile,'wt') as f: f.write(htmlString) 

veya aklınıza gelebilecek bu konuyu sabit? –

+0

Ayrıca bana boş bir png dosyası veriyor. Bu herhangi bir yerde belgelenmiş mi? –

+0

Hayır. Hiç PhantomJS kullanmamıştım, ancak dosya: ///, tarayıcılara bir dosyaya gitmek için ne kullanıyor. Yukarıdaki satırdan kaçmadım (ve şimdi onu düzenleyemem) ... kaçtığından emin oldun mu? –

cevap

11

PhantomJS

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

Bu phantomJS bir ekran görüntüsü almak için nasıl, ben bir süredir phantomJS kullandım.

Sen here.

Selenyum fazla bilgi bu yardımcı olur

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

Umut bulabilirsiniz.

+0

Noktalı virgüllerinizi Selenium (Python) bölümünden kaldırmalısınız. – man2xxl

+0

Yapmak zorunda değilsiniz –

2

PhantomJS

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

Sen 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(); 
     }); 
    }); 
}); 
4

Saf eski güzel piton kullanılarak page.content O zaman işlemek kullanarak sayfanın içeriğini ayarlamak - set t Açılan herhangi bir sayfada hedef html'ye içerikler - JS aracılığıyla. senin örnek kod alarak:

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" 
1

O oluşturulan dosya boş olarak çizgiler

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

, sorunlu Are görünüyor. /// pathtofile/tmp.html`:

Ben

f.close() to your code