2017-08-04 3 views
0

Verwenden Sie python und selenium, ist es möglich, Text zum Screenshot hinzuzufügen? Ich sah, dass dies getan werden könnte, wenn man Dinge in java macht, aber nichts gesehen hat, wenn man python verwendet.Ist es möglich, Text zu Screenshot mit Python Selen hinzuzufügen?

+0

Dies verdient jedoch eine Antwort :) – Nabin

+5

Sobald Sie Ihr Bild mit Selen erstellen, können Sie PIL verwenden. 'importiere PIL' und füge deinen Text über den folgenden Link hinzu. [Schreiben von Text auf Bild mit Hilfe von pil] (https://stackoverflow.com/questions/8154825/how-can-i-write-text-over-an-image-and-overlay-another-image-on-it-in -python) – Haranadh

Antwort

0

Verwendung PIL Bildmodul lesen PIL Bildmodul Dokumentation Here

from PIL import Image 
from PIL import ImageDraw 
from PIL import ImageFont 

img = Image.open("screen.png") 
draw = ImageDraw.Draw(img) 
font = ImageFont.truetype("clear_san.ttf", 23) 
sample_text="hello world!" 
draw.text((100, 100),sample_text,(28,28,28),font=font) 
#here (100,100) is the x,y location of the text and (28,28,28) is the color 
#of font which is drawn on canvas 
img.save("final.png")#saving the image 
#see the image attached 

enter image description here

0

Sie können so etwas tun, Python Selen und ein bisschen (embedded) mit Hilfe von Javascript:

from selenium import webdriver 

def main(): 

    URL = 'https://www.google.com' 
    SAVE_PATH = '/path/to/screenshots/screenshot.png' 
    browser = webdriver.Firefox() 
    browser.get(URL) 
    browser.execute_script(script()) 
    browser.save_screenshot(SAVE_PATH) 

def script(): 

    return 'myCanvas = document.createElement("canvas");'\ 
    + 'myCanvas.id = "myCanvas";'\ 
    + 'myCanvas.width = document.body.clientWidth;'\ 
    + 'myCanvas.height = 60;'\ 
    + 'document.body.insertBefore(myCanvas, document.body.firstChild);'\ 
    + 'var ctx = myCanvas.getContext("2d");'\ 
    + 'ctx.font = "50px Monospace";'\ 
    + 'displayedText = "Hello World!";'\ 
    + 'ctx.strokeText(displayedText, document.body.clientWidth/2 - 150, 50);' 

if __name__ == "__main__": 
    main() 

Beachten Sie jedoch, dass es nicht für jede Site gut funktioniert und abhängig vom Wert von displayedText, müssen Sie die hardcoded v Anzahl der Parameter myCanvas.height und ctx.strokeText, um es richtig zu machen.

+0

das ist wirklich interessant – user655489

Verwandte Themen