PyQT браузер и встроенный внутри yandex iframe. Как «починить» кнопку внутри iframe для принятия платежей?

После нажатия на кнопку "Оплатить" ничего не происходит. Кнопка словно не работает.
Нужно сделать так, чтобы после нажатия на кнопку "Оплатить" перекидывало в браузер по умолчанию в ОС для конечной оплаты.
Я добавил в код рядом тестовую ссылку. С ней все работает. Открывается в браузере ОС ссылка. А с кнопкой - нет.
В сумме у меня уже ушло часов 6 на попытки создания всех возможных костылей. :(.
Мне уже даже не важно на какой версии будет пример - на pyqt4 или pyqt5. Лишь бы свет в конце туннеля увидеть.
Привожу код как на PyQt5 так и на PyQt4 после.

Код на PyQt5:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import webbrowser
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *

class MainWindow(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.resize(700, 400)
        self.view = QWebEngineView()
        s = str("""<!DOCTYPE html>
                                 <html>
                                <head></head>
                                 <div style="text-align: center;">
                                 <iframe frameborder="0" allowtransparency="true" scrolling="no" src="https://money.yandex.ru/quickpay/shop-widget?account=410013878567203&quickpay=shop&payment-type-choice=on&mobile-payment-type-choice=on&writer=seller&targets=asdf&targets-hint=&default-sum=&button-text=01&successURL="" width="450" height="200"></iframe>
                                 </div>
                                  <div>
<a href="http://google.com">TEST LINK</a>
                                 </div>
                                 </body>
                                 </html>""")
        self.view.setHtml(s)
        self.view.urlChanged['QUrl'].connect(self.linkClicked)
        grid = QGridLayout()
        grid.addWidget(self.view, 0, 0)
        self.setLayout(grid)


    def linkClicked(self, url):
        print(url.toString())
        webbrowser.open(url.toString())

if __name__ == '__main__':
    app = None
    if not QApplication.instance():
        app = QApplication([])
    dlg = MainWindow()
    dlg.show()
    if app: app.exec_()


Тот же код на PyQt4:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import webbrowser
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import *

class MainWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.resize(700, 400)
        self.view = QtWebKit.QWebView()
        s = str("""<!DOCTYPE html>
                                 <html>
                                <head></head>
                                 <div style="text-align: center;">
                                 <iframe frameborder="0" allowtransparency="true" scrolling="no" src="https://money.yandex.ru/quickpay/shop-widget?account=410013878567203&quickpay=shop&payment-type-choice=on&mobile-payment-type-choice=on&writer=seller&targets=asdf&targets-hint=&default-sum=&button-text=01&successURL="" width="450" height="200"></iframe>
                                 </div>
                                  <div>
<a href="http://google.com">TEST LINK</a>
                                 </div>
                                 </body>
                                 </html>""")
        self.view.setHtml(s)
        self.view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
        self.view.page().setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateExternalLinks)
        self.connect(self.view, QtCore.SIGNAL("linkClicked(const QUrl)"), self.linkClicked)
        grid = QtGui.QGridLayout()
        grid.addWidget(self.view, 0, 0)
        self.setLayout(grid)


    def linkClicked(self, url):
        print(url.toString())
        webbrowser.open(url.toString())

if __name__ == '__main__':
    app = None
    if not QtGui.QApplication.instance():
        app = QtGui.QApplication([])
    dlg = MainWindow()
    dlg.show()
    if app: app.exec_()
  • Вопрос задан
  • 1268 просмотров
Решения вопроса 1
@Sergey6661313
Этот вариант на pyqt5 работает. Проверено.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import webbrowser
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *

s = str("""<!DOCTYPE html>
         <html>
        <head></head>
         <div style="text-align: center;">
         <iframe frameborder="0" allowtransparency="true" scrolling="no" src="https://money.yandex.ru/quickpay/shop-widget?account=410013878567203&quickpay=shop&payment-type-choice=on&mobile-payment-type-choice=on&writer=seller&targets=asdf&targets-hint=&default-sum=&button-text=01&successURL="" width="450" height="200"></iframe>
         </div>
          <div>
        <a href="http://google.com">TEST LINK</a>
         </div>
         </body>
         </html>""")


class MainWindow(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.resize(700, 400)
        self.view = QWebEngineView(self)

        mypage = MyPage(self.view)
        self.view.setPage(mypage)
        mypage.setHtml(s)

        grid = QGridLayout()
        grid.addWidget(self.view, 0, 0)
        self.setLayout(grid)
        self.show()

class MyPage(QWebEnginePage):
    def __init__(self, parent):
        super().__init__(parent)
        self.in_window = False      # придумал переменную

    def createWindow(self, type):   # которую мы
        self.in_window = True       # тутже изменяем если просится
        return self                 # открытие в новом окне

    def acceptNavigationRequest(self, QUrl, type, isMainFrame):
        url_string = QUrl.toString()
        print(type, isMainFrame, QUrl)
        if  self.in_window and type==2 and url_string != "https://money.yandex.ru/quickpay/confirm.xml":
            webbrowser.open(url_string)
            self.in_window = False
            self.setHtml(s)
        return True




if __name__ == '__main__':
    app = None
    if not QApplication.instance():
        app = QApplication([])
    dlg = MainWindow()
    if app: app.exec_()
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы