Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[问题] qwebengineview 如何同网站多账号登录? #88

Closed
625781186 opened this issue Aug 23, 2019 · 1 comment
Closed

[问题] qwebengineview 如何同网站多账号登录? #88

625781186 opened this issue Aug 23, 2019 · 1 comment
Labels

Comments

@625781186
Copy link
Collaborator

625781186 commented Aug 23, 2019

Environment : / 环境

  • OS: [e.g. Win 10]
  • Python [e.g. 3.6.4 X64]
  • PyQt5 [e.g. 5.10.1]
# Ui_man.py
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 522)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.webTabWidget = QtWidgets.QTabWidget(self.centralwidget)
        self.webTabWidget.setTabShape(QtWidgets.QTabWidget.Rounded)
        self.webTabWidget.setElideMode(QtCore.Qt.ElideLeft)
        self.webTabWidget.setDocumentMode(False)
        self.webTabWidget.setTabsClosable(True)
        self.webTabWidget.setMovable(True)
        self.webTabWidget.setObjectName("webTabWidget")
        self.tab = QtWidgets.QWidget()
        self.tab.setObjectName("tab")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.tab)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.web1 = QtWebEngineWidgets.QWebEngineView(self.tab)
        self.web1.setUrl(QtCore.QUrl("https://www.baidu.com/"))
        self.web1.setObjectName("web1")
        self.horizontalLayout_3.addWidget(self.web1)
        self.webTabWidget.addTab(self.tab, "")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.tab_2)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.web2 = QtWebEngineWidgets.QWebEngineView(self.tab_2)
        self.web2.setUrl(QtCore.QUrl("https://www.youtube.com/"))
        self.web2.setObjectName("web2")
        self.horizontalLayout_2.addWidget(self.web2)
        self.webTabWidget.addTab(self.tab_2, "")
        self.horizontalLayout.addWidget(self.webTabWidget)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.urlBar_toolBar = QtWidgets.QToolBar(MainWindow)
        self.urlBar_toolBar.setMovable(False)
        self.urlBar_toolBar.setAllowedAreas(QtCore.Qt.TopToolBarArea)
        self.urlBar_toolBar.setFloatable(False)
        self.urlBar_toolBar.setObjectName("urlBar_toolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.urlBar_toolBar)
        self.bookMarkBar_toolBar = QtWidgets.QToolBar(MainWindow)
        self.bookMarkBar_toolBar.setMovable(False)
        self.bookMarkBar_toolBar.setAllowedAreas(QtCore.Qt.TopToolBarArea)
        self.bookMarkBar_toolBar.setFloatable(False)
        self.bookMarkBar_toolBar.setObjectName("bookMarkBar_toolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.bookMarkBar_toolBar)
        MainWindow.insertToolBarBreak(self.bookMarkBar_toolBar)

        self.retranslateUi(MainWindow)
        self.webTabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.webTabWidget.setTabText(self.webTabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
        self.webTabWidget.setTabText(self.webTabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
        self.urlBar_toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
        self.bookMarkBar_toolBar.setWindowTitle(_translate("MainWindow", "toolBar_2"))

from PyQt5 import QtWebEngineWidgets
# main.py
# -*- coding: utf-8 -*-

"""
Module implementing MainWindow.
mdi icon :https://cdn.materialdesignicons.com/3.8.95/

pip install qtpy qtawesome
"""
from PyQt5.QtNetwork import QNetworkAccessManager
from PyQt5.QtWebEngineWidgets import QWebEngineProfile, QWebEngineView

from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
# import qtawesome as qta

# from PyQt5.QtWidgets import QMainWindow

try:
    from Ui_main import Ui_MainWindow
except ImportError as e:
    print("--1 --", e)
    try:
        from .Ui_main import Ui_MainWindow
    except ImportError as e:
        print("--2 --", e)


class MainWindow(QMainWindow, Ui_MainWindow):
    """
    Class documentation goes here.
    """

    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        # apn_icon = qta.icon('mdi.gamepad-round', color='green')

        self.__newTabButton = QToolButton(self)
        # self.__newTabButton.setIcon(apn_icon)
        # self.__newTabButton.setProperty('icon', apn_icon)
        self.__newTabButton.setToolTip(self.tr("Open a new web browser tab"))
        self.webTabWidget.setCornerWidget(self.__newTabButton, Qt.TopLeftCorner)

        # webEngineProfile = QWebEngineProfile.defaultProfile()
        # cookieStore = webEngineProfile.cookieStore()


        for i in range(1, 3):
            webview = getattr(self, 'web' + str(i))  # type:QWebEngineView
            if webview:
                self.set_cache(webview, i)
                webview.setUrl(QUrl("http://github.com"))

    def set_cache(self, webview: QWebEngineView, number: int):
        networkManager = QNetworkAccessManager()
        cookieJar = networkManager.cookieJar()
        # cookies = cookieJar.cookiesForUrl(strUrl)
        web_profile = webview.page().profile()
        print(web_profile)
        cookieStore = web_profile.cookieStore()
        # cookieStore.setCookie(cookies.front(), url)

        COOKIPATH = "./Cache" + str(number)
        # web_profile.setCachePath(COOKIPATH)
        web_profile.setPersistentStoragePath(COOKIPATH)
        web_profile.setPersistentCookiesPolicy(2)
        # self.web1.page().load(url)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setStyle(QStyleFactory.create("Fusion"))

    #    自定义CSS样式
    #    from BasePack.CommonHelper import CommonHelper
    #    styleFile = 'BasePack/style.css'
    #    qssStyle = CommonHelper.readQss( styleFile )
    #    framelessWindow.setStyleSheet( qssStyle )

    #    If you want to use this style, please pip install qdarkstyle.
    #    import qdarkstyle
    #    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())

    ui = MainWindow()

    ui.show()
    sys.exit(app.exec_())

写不来 . 2个tab还是用了同一份coockie

892768447 added a commit that referenced this issue Aug 23, 2019
@892768447
Copy link
Member

原理是为每个QWebEngineView创建一个QWebEnginePage,且使用独立的QWebEngineProfile,并配置persistentStoragePath不同路径

https://github.com/PyQt5/PyQt/blob/master/QWebEngineView/SiteDiffUser.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants