我的QWebEngineView停在Instagram加载界面上,不接受重新加载等输入。

huangapple go评论90阅读模式
英文:

Why my QWebEngineView stucks at instagram loading screen and don't accept inputs like reload

问题

当我运行代码时,一切都正常,除了停在Instagram的闪屏/加载屏幕上,甚至右键菜单选项也不起作用。对于PyQt5,一切正常,但不适用于PyQt6。与互联网连接无问题。

from PyQt6.QtWidgets import *
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWebEngineCore import QWebEnginePage, QWebEngineProfile
from PyQt6.QtCore import QUrl

class Main(QWebEngineView):
    def __init__(self):
        super().__init__()
        self.profile = QWebEngineProfile("data", self) # 创建一个具有self为父项的离线配置文件
        self.profile.setPersistentCookiesPolicy(QWebEngineProfile.PersistentCookiesPolicy.AllowPersistentCookies) # 强制将Cookie存储在磁盘上
        self.profile.setHttpCacheType(QWebEngineProfile.HttpCacheType.DiskHttpCache) # 缓存存储在磁盘上
        self.profile.setPersistentStoragePath('data') # Cookie路径在根目录的data文件夹中
        self.profile.setCachePath('data') # 缓存路径在根目录的data文件夹中

        self.page = QWebEnginePage(self.profile) # 使用上述配置文件创建一个页面
        self.setPage(self.page) # 将页面设置为QWebEngineView的页面
        self.load(QUrl("https://www.instagram.com")) # 在视图中加载URL
        self.show()

app = QApplication(sys.argv)
main = Main()
app.exec()

输出

我尝试打开Google,我想只有登录按钮有效。在PyQt5中,一切正常,除了一点点JS。我期望网站加载并且可以输入。

英文:

When I run everything works fine except it stops at instagram splash/loading screen, even the right click menu option don't work. Works for PyQt5 but not from PyQt6.There is no problem with internet connection.

from PyQt6.QtWidgets import *
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWebEngineCore import QWebEnginePage, QWebEngineProfile
from PyQt6.QtCore import QUrl


class Main(QWebEngineView):
    def __init__(self):
        super().__init__()
        self.profile = QWebEngineProfile("data", self) # Create a off-road profile with parent self
        self.profile.setPersistentCookiesPolicy
        (QWebEngineProfile.PersistentCookiesPolicy.AllowPersistentCookies) # Force the cookie to be stored in disk
        self.profile.setHttpCacheType(QWebEngineProfile.HttpCacheType.DiskHttpCache) # Cache is stored in disk
        self.profile.setPersistentStoragePath('data') # Cookie path in data folder in root
        self.profile.setCachePath('data') # Cache path in data folder in root

        self.page = QWebEnginePage(self.profile) # Create a page with profile from above
        self.setPage(self.page) # not set page to QWebEngineView
        self.load(QUrl("https://www.instagram.com")) # load the url in view
        self.show()


app = QApplication(sys.argv)
main = Main()
app.exec()

output

I tried opening google I guess only sign in button worked. In PyQt5 everything work except a little bit JS.
I expect the site to get load and it take input.

答案1

得分: 1

这似乎是QtWebEngine的一个错误。(怎么样在这里报告这个问题呢?)您可以通过使用绝对路径来解决这个问题,如下所示。

import os
...
class Main(QWebEngineView):
    def __init__(self):
        ...
        self.profile.setPersistentStoragePath(os.path.abspath('data'))
        self.profile.setCachePath(os.path.abspath('data'))
        ...
英文:

This seems like a bug of QtWebEngine.(What about reporting this on here?) You can remedy the problem by using absolute paths like this.

import os
...
class Main(QWebEngineView):
    def __init__(self):
        ...
        self.profile.setPersistentStoragePath(os.path.abspath('data'))
        self.profile.setCachePath(os.path.abspath('data'))
        ...

huangapple
  • 本文由 发表于 2023年6月19日 21:58:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定