Search code examples
pythonseleniumtkinterselenium-chromedriverscreenshot

Taking selenium screenshots and displaying on tkinter gui


I'm trying to take a screenshot of a selenium page and display it on a canvas on my tkinter gui, but for some reason I get the error:

TypeError: __str__ returned non-string (type bytes)

This is the code, thanks for any help in advance

from tkinter import *
import tkinter as tk
import time
from selenium import webdriver

root = tk.Tk()
root.geometry('700x700')


def picture():
    browser = webdriver.Chrome('C:\\Users\\Downloads\\chromedriver_win324\\chromedriver.exe')
    browser.get('https://google.co.uk')
    fil = browser.get_screenshot_as_png()
    img = PhotoImage(fil)
    canvas = Canvas(root, width=300,height=300)
    canvas.place(x=12.5,y=450)
    canvas.create_image(20,20, image=img)

Button(root, text='Start', command=lambda: picture()).pack()
root.mainloop()

Solution

  • The problem is that you're trying to use the data as the first positional parameter to PhotoImage, but the first positional parameter is for the name rather than the image itself.

    You need to provide the data as the value for the data attribute:

    img = PhotoImage(data=fil)