Python PyQt5 unable to load some pages correctly

I try to load a page in a webview , so i can interact with it and then parse some data.The page i want to interact and parse must be created from the start page(https://www.public.nm.eurocontrol.int/PUBPORTAL/gateway/spec/). My problem is even if create window will open the page it wont load almost anything.

The link that opens the page i want to open (if you scroll down to the right) is STRUCTURED EDITOR:

enter image description here

My script:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWebKitWidgets import QWebView , QWebPage
from PyQt5 import QtWebKit
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtNetwork import *
import sys
import time



class MYview(QWebView):

    def __init__(self, parent=None):
        super(MYview, self).__init__(parent)

    def createWindow(self,wintype):
            print("aaaaaaaaaaaaa")
            self.webView = MYview()
            time.sleep(2)
            self.webView.page().settings().globalSettings().setAttribute(QWebSettings.JavascriptCanOpenWindows, True)
            self.webView.page().settings().globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)
            self.webView.page().settings().globalSettings().setAttribute(QWebSettings.JavascriptEnabled, True)
            self.webView.page().settings().globalSettings().setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True)
            self.webView.page().settings().globalSettings().setAttribute(QWebSettings.LocalContentCanAccessFileUrls, True)
            self.webView.page().settings().globalSettings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
            self.webView.loadFinished.connect(self._on_load_finished)
            return self.webView
    def javaScriptConsoleMessage(self, msg, line, source):
        print ('%s line %d: %s')%(source, line, msg)

    def _on_load_finished(self, ok):
        print(self.webView.url())
        self.webView.loadFinished.disconnect(self._on_load_finished)


class MainForm(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.tabWidget = QtWidgets.QTabWidget(self)        
        self.setCentralWidget(self.tabWidget)        
        self.loadUrl(QtCore.QUrl('https://www.public.nm.eurocontrol.int/PUBPORTAL/gateway/spec/'))

    def loadUrl(self, url):    
        self.view = MYview()
        self.view.page().settings().globalSettings().setAttribute(QWebSettings.JavascriptCanOpenWindows, True)
        self.view.page().settings().globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)
        self.view.page().settings().globalSettings().setAttribute(QWebSettings.JavascriptEnabled, True)
        self.view.page().settings().globalSettings().setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True)
        self.view.page().settings().globalSettings().setAttribute(QWebSettings.LocalContentCanAccessFileUrls, True)
        self.view.page().settings().globalSettings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
        self.view.loadFinished.connect(self._on_load_finished)
        self.view.linkClicked.connect(self.on_linkClicked)
        # self.view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
        self.tabWidget.setCurrentIndex(self.tabWidget.addTab(self.view, 'loading...'))
        self.view.load(url)

    def _on_load_finished(self, ok):
        self.view.loadFinished.disconnect(self._on_load_finished)
        index = self.tabWidget.indexOf(self.sender())
        self.tabWidget.setTabText(index, self.sender().url().host())
        self.view.page().mainFrame().evaluateJavaScript("document.getElementsByTagName('button')[0].click();")



    def on_linkClicked(self, url):
        print('link clicked to {}'.format(url))    
        self.loadUrl(url)


    def javaScriptConsoleMessage(self, msg, line, source):
        print ('%s line %d: %s')%(source, line, msg)



def main():
    app = QtWidgets.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

Some pages that is else where opens just fine , but all pages need to portal from main site don't show.Maybe it has something to do with pathing but i cant figure it out

Upvotes: 0

Views: 351

Answers (1)

musicamante
musicamante

Reputation: 48424

The problem is that the new window seems to be created using a network POST request, and you need to inherit it from the page that creates it.

Note that you should keep track of existing windows and delete them on close, otherwise if an existing page tries to load into a target that still exists (even if you closed the window) no new page will be opened.

class MYview(QWebView):
    views = []

    def __init__(self, parent=None):
        super(MYview, self).__init__(parent)
        self.views.append(self)

    def createWindow(self,wintype):
        webView = MYview()
        webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
        webView.destroyed.connect(lambda w, view=webView: self.views.remove(view))
        webView.page().setNetworkAccessManager(self.page().networkAccessManager())
        return webView

I also removed the settings, since you've already done that for the global settings in the main window, there's no need to set them again.

Upvotes: 1

Related Questions