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

Passing cookies to QWebEngineView using python #155

Closed
godomainz opened this issue May 10, 2022 · 5 comments
Closed

Passing cookies to QWebEngineView using python #155

godomainz opened this issue May 10, 2022 · 5 comments
Labels

Comments

@godomainz
Copy link

so I have a python code which converts url to pdf like below

import sys
from PyQt5 import QtWidgets, QtWebEngineWidgets
from PyQt5.QtCore import QUrl, QTimer
from PyQt5.QtGui import QPageLayout, QPageSize
from PyQt5.QtWidgets import QApplication
import argparse



def _fullScreenRequested(request):
    request.accept()
    loader.showFullScreen()

def main():
    url = ''
    parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--url", help="Type url")
    args = parser.parse_args()
    config = vars(args)
    url = config['url']


    app = QtWidgets.QApplication(sys.argv)
    loader = QtWebEngineWidgets.QWebEngineView()
    loader.setZoomFactor(1)
    layout = QPageLayout()
    layout.setPageSize(QPageSize(QPageSize.A4Extra))
    layout.setOrientation(QPageLayout.Portrait)
    loader.load(QUrl(url))
    loader.page().pdfPrintingFinished.connect(lambda *args: QApplication.exit())

    def emit_pdf(finished):
        # loader.page().printToPdf("test.pdf", pageLayout=layout)
        QTimer.singleShot(2000, lambda: loader.page().printToPdf("test.pdf", pageLayout=layout))

    loader.loadFinished.connect(emit_pdf)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

I have a cookie.txt with the content below

  [
    {
        "domain": "www.udemy.com",
        "expirationDate": 1714906174.734258,
        "hostOnly": true,
        "httpOnly": false,
        "name": "snexid",
        "path": "/",
        "sameSite": null,
        "secure": false,
        "session": false,
        "storeId": null,
        "value": "c6sdf99-1sdab-4sd1-86ff-2dc8sfs24511"
    }
]

is there a way to pass my cookie.txt to QWebEngineView or QtWebEngineWidgets ??

@892768447
Copy link
Member

https://doc.qt.io/qt-5/qwebenginecookiestore.html

add cookie for url

@godomainz
Copy link
Author

@892768447 any sample code on how to do that ?

@892768447
Copy link
Member

#88

cookieStore.setCookie(cookies.front(), url)

@godomainz
Copy link
Author

godomainz commented May 12, 2022

@892768447 as per your suggestion I changed the code like below

import sys
from PyQt5 import QtWidgets, QtWebEngineWidgets
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkCookie
from PyQt5.QtCore import QUrl, QTimer
from PyQt5.QtGui import QPageLayout, QPageSize
from PyQt5.QtWidgets import QApplication
import argparse

def main():
    url = ''
    parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--url", help="Type url")
    args = parser.parse_args()
    config = vars(args)
    url = config['url']


    app = QtWidgets.QApplication(sys.argv)
    loader = QtWebEngineWidgets.QWebEngineView()
    networkManager = QNetworkAccessManager()
    cookieJar = networkManager.cookieJar()
    cookies = cookieJar.cookiesForUrl(QUrl(url))
    web_profile = loader.page().profile()
    cookieStore = web_profile.cookieStore()
    cookieStore.setCookie(cookies.front(), QUrl(url))
    loader.setUrl(QUrl(url))
    loader.setZoomFactor(1)
    layout = QPageLayout()
    layout.setPageSize(QPageSize(QPageSize.A4Extra))
    layout.setOrientation(QPageLayout.Portrait)
    loader.load(QUrl(url))
    loader.page().pdfPrintingFinished.connect(lambda *args: QApplication.exit())

    def emit_pdf(finished):
        QTimer.singleShot(2000, lambda: loader.page().printToPdf("test.pdf", pageLayout=layout))


    loader.loadFinished.connect(emit_pdf)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

I'm getting below error

Traceback (most recent call last):
  File ".\htmlToPdfnew.py", line 41, in <module>
    main()
  File ".\htmlToPdfnew.py", line 21, in main
    cookie.path("cookie.txt")
TypeError: path(self): too many arguments
PS F:\Projects\E2E\htmlToPDF> python.exe .\htmlToPdfnew.py --url https://vm2.qa02.oneit.com.au/gds/
Traceback (most recent call last):
  File ".\htmlToPdfnew.py", line 42, in <module>
    main()
  File ".\htmlToPdfnew.py", line 25, in main
    cookieStore.setCookie(cookies.front(), QUrl(url))
AttributeError: 'list' object has no attribute 'front'

btw how can we specify the file path for "cookie.txt" ?

@892768447
Copy link
Member

892768447 commented May 12, 2022

File ".\htmlToPdfnew.py", line 25, in main
    cookieStore.setCookie(cookies.front(), QUrl(url))
AttributeError: 'list' object has no attribute 'front'

change to  cookies[0]
webEngineProfile = QWebEngineProfile.defaultProfile()
cookieStore = webEngineProfile.cookieStore()

#https://doc.qt.io/qt-5/qwebenginecookiestore.html#setCookie
#cookieStore.setCookie(const QNetworkCookie &cookie, const QUrl &origin = QUrl())

cook = QNetworkCookie()
cook.setDomain('ww.udemy.com')
#cook.setExpirationDate(QDateTime) # date
cook.setPath('/')
cook.setValue('c6sdf99-1sdab-4sd1-86ff-2dc8sfs24511'.encode())
cookieStore.setCookie(cook , QUrl(url))

you can search QNetworkCookie from github like

https://github.com/dean2021/splash/blob/83383a3abf2e2909bdcc5e68a2d58caf2995f605/splash/cookies.py#L82

892768447 added a commit that referenced this issue May 12, 2022
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