0

I am trying to use Qwebengineview to view a list of youtube videos but the browser doesn't autoplay the videos, I am using PyQt5 5.13.1 Python 3.6

here is a sample code

from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage
from PyQt5.QtWidgets import QApplication

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    webview = QWebEngineView()
    profile = QWebEngineProfile("my_profile", webview)
    profile.defaultProfile().setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies)
    webpage = QWebEnginePage(profile, webview)
    webview.setPage(webpage)
    webview.load(QUrl("https://www.youtube.com/watch?v=VzIVI2fsRb0"))
    webview.show()
    sys.exit(app.exec_())
Mohamed Yousof
  • 725
  • 1
  • 9
  • 38

1 Answers1

2

I have find a solution for this using QWebEngineSettings and here is a full working example in case someone needs it

from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage, QWebEngineSettings
from PyQt5.QtWidgets import QApplication
import time

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    webview = QWebEngineView()
    profile = QWebEngineProfile("my_profile", webview)
    profile.defaultProfile().setPersistentCookiesPolicy(QWebEngineProfile.ForcePersistentCookies)
    webpage = QWebEnginePage(profile, webview)
    webpage.settings().setAttribute(QWebEngineSettings.PlaybackRequiresUserGesture, False)

    webview.setPage(webpage)
    webview.load(QUrl("https://www.youtube.com/watch?v=aKCNrkERJ3E"))
    webview.show()

    sys.exit(app.exec_())
Mohamed Yousof
  • 725
  • 1
  • 9
  • 38