简体   繁体   中英

Logging into website failure

I did look into tutorials obviously but the html I have is different and I tried a lot of things (the code I will post is the last thing I tried). I will provide you with the html too.

   WebBrowser1.Navigate("THE_WEBSITE")
    WebBrowser1.Document.GetElementById("tbtLoginID").SetAttribute("onfocus", "javascript:$(this).val('ID_GOES_HERE');$('#wrong_password_board').css('display','none');")
    WebBrowser1.Document.GetElementById("tbtPassword").SetAttribute("value", "PASS_PROBABLY_GOES_HERE")
    WebBrowser1.Document.GetElementById("div_123").SetAttribute("onpress", "rockLogin($('#loginform'))")'

And this is the HTML (I think it's everything that matters, the website is www.microvolts.com anyway)

<form id="loginform" action="/Account/login" method="POST">
        <div class="left1"> </div>
        <div class="login_info1">
            <div class="icon_rh">
            </div>
            <div class="floatleft" style="margin-bottom: 2px;">
                <input id="tbtLoginID" width="110px" maxlength="25" height="17px" class="text_box" value="Login ID" tabindex="1" onfocus="javascript:$(this).val('');$('#wrong_password_board').css('display','none'); "> 
            </div>
            <div class="floatleft">
                <div id="loginLoginIdDiv1">
                    <input type="text" onfocus="javascript:$(this).val('');$('#wrong_password_board').css('display','none'); " value="Password" tabindex="2" class="text_box" name="passwordFaked" size="20" width="110px" style="display: none;">
                    <div id="loginPasswordDiv2" style="">
                        <input id="tbtPassword" width="110px" type="password" tabindex="2" maxlength="25" height="17px" class="text_box"> 
                    </div>
                </div>

                <div id="wrong_password_board" class="wrong_password wrong_passwordid ErrorMsg" style="display: none;">
            <div class="topleft"></div>
            <div class="topmiddle">Invalid Login ID and/or Password.</div>
            <div class="topright"></div>
        </div>
            </div>
        </div>
        <div class="login_info2">
            <div class="row1">
                <a href="javascript:void(0);" onclick="fbConnect();" class="login_facebook"><span class="left"></span><span class="middle">Login</span><span class="right"></span></a>
            </div>
            <div class="row1" style="width: 80px">
                <span style="float: left; margin-right: 3px;">
                    <div id="div_123" class="toplogin RockButton" style="width: 60px; height: 19px;">
                        <div id="123" onclick="rockLogin($('#loginform'))" style="width: 100%; height: 100%;" class="rbtdiv">
                            <div class="text">
                                Login</div>
                        </div>
                        <div class="rbtmask" style="z-index: 100; position: absolute; display: none; background: grey;
                            opacity: 0.2; cursor: default; width: 100%; height: 100%; top: 0px; left: 0px;">
                        </div>
                    </div>
                </span>
                <div class="btn_question forgetPasswordButton ">

                    <div id="ifClickThisBtn" style=" display:none;" data-ifclick="false"></div>
                </div>
                <div id="forgetPassword" class="tip_forgot_password">
                            <div class="ajaxLoadingMask">
                                <img src="/images/ajaxLoading2.gif" style="margin-left: 150px; margin-top: 9px; position: absolute; display: block;" class="ajaxLoading" alt="">
                            </div>
                            <div class="lefttfp">
                                <div class="l1l"></div>
                                <div class="l1r"></div>
                            </div>
                            <div class="middletfp">
                                <div class="closetfp"></div>
                                <div class="title">Forgot Password or Login ID</div>
                                <div class="email_form">
                                    <input type="text" value="Enter registered email" name="emailAddress" class="password_recovery_textbox">

                                    <div class="wbtn1 formSubmitBtn" onclick="javascript:forgetPassword();return false;" style="float: left; margin: 2px 0px 0px 5px; cursor: pointer;">
                                   <span class="wbtn1_left"></span><span class="wbtn1_middle" style="padding-left: 10px;
                                    padding-right: 10px;">Submit</span><span class="wbtn1_right"></span></div>
                                    <div class="wrong_email"></div>
                                </div>
                                <div id="invalideEmailAddress" style="display:none;"> Please enter a valid email address.</div>
                            </div>
                            <div class="righttfp"></div>
                        </div>
            </div>
        </div>
        <div class="right1"> </div>

        </form>

The login form has javascript elements so I figured I could just write it with the right login into it, but nah. When I run it I get an error from the first line: System.NullRefferenceException , all my codes gave that.

I have to admit I probably didn't do all the research I could but I need to do this fast and this is the last thing that may help me.

As already mention above in the comment's by @Visual Vincent you need to add the DocumentCompleted event. This event is raised when the document has finished loading.

Private docCompleted As Boolean = False 'put this at class level

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If docCompleted Then Exit Sub
        If WebBrowser1.Document.GetElementById("tbtLoginID") IsNot Nothing Then
            WebBrowser1.Document.GetElementById("tbtLoginID").SetAttribute("value", "YOURUSERNAME")
        End If
        If WebBrowser1.Document.GetElementById("tbtPassword") IsNot Nothing Then
            WebBrowser1.Document.GetElementById("tbtPassword").SetAttribute("value", "YOURPASSWORD")
        End If
        If WebBrowser1.Document.GetElementById("div_123") IsNot Nothing Then
            WebBrowser1.Document.InvokeScript("rockLogin")
        End If

        docCompleted = True
    End Sub

The code above check's each element to ensure it not nothing and if it's not then we can set the attributes we would need. Also we can InvokeScript of the document for example: rockLogin , this script does the login. The docCompleted prevents the page from doing postback's as when it's finished and you set the attributes it want's to reload the page etc... it's not required but it prevented multiple postbacks when I tried.

On another note you didn't mention if you were doing this in a click event or what, if so set the docCompleted to false before navigating to your page so the variable is reset each time...

Note: Tried and Tested - please don't post your login credentials either on a public forum...

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