Tanukaum
Tanukaum

Reputation: 46

How to fill login forms with QWebEngineView with code only?

I am trying to integrate a web page from a server into my GUI, the user should be able to view, scroll, interact with the page in some way. But to access the page, a login needs to be done.

The login has two steps, a 'pop-up' window that requires clicking a button, some fields need to be filled in, and then another button clicked. Only then can I fill in the username/password.

My problem is that I am not able to access the required fields. My fields are inside an iframe-> #document -> html -> form... as shown below

html xmlns="http://www.w3.org/1999/xhtml">
<head>//not relevant</head> 
<body class="ContentArea">
    <form method="post" action="./Login" onsubmit="javascript:return WebForm_OnSubmit();" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'LoginMain_LoginButton')" id="MyIframe1ID">
        <div id="DIV1">
            <table>
                <tbody>
                    <tr class="ContentRow">
                        <td class="WindowContent">
                            <iframe name="IWantThisContent" src="someDomain.aspx">
                                #document (http://mylink/someDomain.aspx)
                                <html xmlns="http://www.w3.org/1999/xhtml" class="INeedToFillContentHere">
                                    <body>
                                        <form method="post" accept=".someDomain.aspx" id="MyIframe2ID">
                                            <div class="DialogMYDIV">

                                             my first fields and buttons are here

                                            </div>
                                        </form>

I've tried and failed A LOT, I couldn't even click the damn button:

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setGeometry(100,100,800,600)

        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl('site.com'))

        self.browser.loadFinished.connect(self.load_finished)

    def load_finished(self):
        ## contentDocument and documentWindow Failed
        #Failed js: Uncaught TypeError: Cannot read property 'getElementById' of undefined
        self.browser.page().runJavaScript("let iframe = document.getElementById('MyIframe1ID'); iframe.contentDocument.getElementById('mybutton').click()")

        #Failed js: Uncaught TypeError: Cannot read property 'getElementById' of undefined
        self.browser.page().runJavaScript("let iframe = document.getElementById('MyIframe2ID'); iframe.contentDocument.getElementById('mybutton').click()")

        #Failed 
        self.browser.page().runJavaScript("document.getElementById('mybutton').click();")

        #Failed
          self.browser.page().runJavaScript("document.getElementById('mybutton')[0].click()")
        
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

Upvotes: 0

Views: 26

Answers (0)

Related Questions