Search code examples
pythonwebpyqtpyqt5http-headers

Python QWebEngine, JS error: Refused to frame * because it violates the following Content Security Policy directive: "frame-src 'self'


I'm setting a browser widget within another application, but some sites practically nothing works with interaction because the JS on the sites returns an error 'violates the following Content Security Policy directive'. Tried to configure using QWebEngineProfile but without result.

import os
import sys
import pystray

from ctypes import windll
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
    
        self.setWindowFlag(QtCore.Qt.Tool)

        self.browser = QWebEngineView()
            
        self.profile = QtWebEngineWidgets.QWebEngineProfile.defaultProfile()

        self.webpage = QWebEnginePage(profile, self.browser)
        self.browser.setPage(self.webpage)

        self.show()

if __name__ == "__main__":    

    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)

    QApplication.setApplicationName('Web Widget')

    window = MWindow()

    app.exec_()     

Solution

  • I got result in this post: Topic , basically my web browser doesn't have "new tab" so its not doing anything. It was not accessing the page, as the site requested to open in a new tab, so this topic shows how to redirect to the current window.