简体   繁体   中英

How to get page element by id using QWebEngineView

How do I get the page ElementById so I can fill the input value?

import sys
from PyQt5 import QtWebEngineWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QAction
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget, QMainWindow


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.iniGUI()  # App GUI
        self.table_widget = TabsWidgets(self)  # tabs
        self.setCentralWidget(self.table_widget)
        self.setWindowIcon(QIcon(r"images\title_icons.png"))

    def iniGUI(self):
        self.setWindowTitle("Team")
        self.resize(1500, 900)  # Width, height
        self.add_guiMenu_widgets()  # top menu

    # main window menu
    def add_guiMenu_widgets(self):
        self.statusBar().showMessage("Team")
        main_menu = self.menuBar()
        file_menu = main_menu.addMenu("File")
        help_menu = main_menu.addMenu("Help")

        # file menu button
        exit_button = QAction(QIcon("exit.png"), "Exit", self)
        exit_button.setShortcut("Ctrl+Q")
        exit_button.setStatusTip("Exit Application")
        exit_button.triggered.connect(QApplication.instance().quit)
        file_menu.addAction(exit_button)

        # help menu button
        about_button = QAction(QIcon("about.png"), "About", self)
        about_button.setShortcut("Ctrl+H")
        about_button.setStatusTip("About Application")
        help_menu.addAction(about_button)


def load_finished():
    print("Web Page is loaded")
    TabsWidgets.mainFlash.page().runJavaScript("document.getElementById('email').Value ='0m3r'")


class TabsWidgets(QWidget):
    def __init__(self, parent):
        super(TabsWidgets, self).__init__(parent)
        self.layout = QVBoxLayout()

        # Initialize tab screen
        self.tabs = QTabWidget()
        self.tab_mainFlash = QWidget()

        # Add tabs
        self.tabs.addTab(self.tab_mainFlash, " Flash - Main ")

        # Create tab_mainFlash
        self.tab_mainFlash.layout = QVBoxLayout(self)
        self.mainFlash = QtWebEngineWidgets.QWebEngineView()
        self.tab_mainFlash.layout.addWidget(self.mainFlash)
        self.tab_mainFlash.setLayout(self.tab_mainFlash.layout)
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)
        self.mainFlash.load(QUrl(
            "https://www.facebook.com/login.php"))
        self.mainFlash.loadFinished.connect(load_finished)

        # self.mainFlash.page().runJavaScript("document.getElementById('email').Value ='0m3r'")
        # self.mainFlash.page().runJavaScript("document.getElementById('pass').Value ='0m3r'")


if __name__ == "__main__":
    application = QApplication(sys.argv)  # create Application
    application.setStyle("Fusion")  # stylesheet
    appGui = MainWindow()  # create instance of class
    appGui.show()  # show the constructed window
    sys.exit(application.exec_())  # execute the application

You have to use the "mainFlash" object but you use the TabsWidgets class which is somewhat illogical (I recommend you review the basic concepts of OOP: class and object/instance). In addition, javascript is case-sensitive, so "value" is different from "Value", and the latter property is not owned by DOM elements.

Considering the above, the solution is:

class TabsWidgets(QWidget):
    def __init__(self, parent):
        # ...

        self.mainFlash.load(QUrl("https://www.facebook.com/login.php"))
        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM