0

The program on PyQt5 + QWebEngineView closes when loading youtube.music site after about 3-4 seconds after opening, without any error, while any other site loads without any problems and the program does not close itself. This problem started today, yesterday youtube.music was opening successfully in QWebEngineView. I'm using windows 10 21h1.

After I realized that the problem is not in my code I tried to write a completely clean code with PyQt5 + QWebEngineView and opening music.youtube.com, but without success.

from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage


app = QApplication([])

window = QMainWindow()
central_widget = QWidget()
layout = QVBoxLayout(central_widget)

webview = QWebEngineView()
page = QWebEnginePage()
webview.setPage(page)
page.load(QUrl("https://music.youtube.com"))
layout.addWidget(webview)

window.setCentralWidget(central_widget)
window.show()

app.exec()

I also tried clearing chrome browser cookies, but still no luck. I don't know what to do. The site itself opens successfully in both Firefox and Chrome. I also tried to go to music.youtube.com through google search engine, but still the same when opening the program closes without error:( What to do? How to fix it? P.S: If you compile this code into an .exe, everything will work properly.

  • I cannot reproduce this with the latest PyQt5 on Linux. Run the script in a terminal or command prompt (not from the IDE) and eventually [edit] your post to provide the full output after running until it crashes. – musicamante Jul 19 '23 at 20:40
  • @musicamante I've run the code in both windows 10 terminal and sublime text 4, the result is the same - no output to the console. I also wrote a code that tracks the page title changes - it is when the page title becomes Youtube Music that the script is stopped. – ツDeeffest笑 Jul 19 '23 at 21:33
  • The whole problem was a QWebEngine cookie. If you find the folder with your QWebEngine cookies in the directory C:\Users\USERNAME\AppData\Local and delete the folder Service Worker in the directory YourAppName\QtWebEngine\Default, everything will work and the sites' data will not be lost. – ツDeeffest笑 Sep 02 '23 at 14:59

1 Answers1

0

After much time and effort, I have found a solution to this problem!

from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage


app = QApplication([])

app.setApplicationName("without setApplicationName, nothing worked!!!")

window = QMainWindow()
central_widget = QWidget()
layout = QVBoxLayout(central_widget)

webview = QWebEngineView()
page = QWebEnginePage()
webview.setPage(page)
page.load(QUrl("https://music.youtube.com"))
layout.addWidget(webview)

window.setCentralWidget(central_widget)
window.show()

app.exec()

It turns out it was enough to add the line app.setApplicationName("name"). If you leave this line empty and do not specify a name, the program will still close without errors or any information:)

I hope I've helped someone.