locked
Help javacript and vb.net webbrowser automation RRS feed

  • Question

  • I'm trying to automate a change password operation on magento but i have a problem.

    I need to programmatically simulate a click on the the texbox newpassword to make the currentpassword confirmation visible.

    i tried everything nothing work !

    ""

    'elmclient.InvokeMember("onchange")
                                'elmclient.Focus()
                                'elmclient.RaiseEvent("change")
                                'elmclient.RaiseEvent("onclick")
                                'elmclient.RaiseEvent("OnClick")
                                'elmclient.InvokeMember("click")

                                elmclient.InvokeMember("Click")

                                elmclient.InvokeMember("Focus")

                                elmclient.InvokeMember("Blur")

                                elmclient.InvokeMember("Keypress")

                                elmclient.InvokeMember("KeyDown")"

    No solution to have the visibility of the current password field 

    i found the javacript function in The source code of the page involved and it is :

     <script type="text/javascript">
    //<![CDATA[
        $$('#_accountnew_password,#account-send-pass').each(function(elem) {
            $(elem).on('change', function() {
                if ($('_accountnew_password').getValue() || $('account-send-pass').checked) {
                    $('_accountcurrent_password_container').show();
                    $('_accountcurrent_password').enable();
                } else {
                    $('_accountcurrent_password_container').hide();
                    $('_accountcurrent_password').disable();
                }
            });
            $(elem).on('focus', function() {
                $('_accountcurrent_password_container').show();
                $('_accountcurrent_password').enable();
            });
            $(elem).on('blur', function() {
                if (!$('_accountnew_password').getValue() && !$('account-send-pass').checked) {
                    $('_accountcurrent_password_container').hide();
                    $('_accountcurrent_password').disable();
                }
            });
            document.observe("dom:loaded", function() {
                $('_accountcurrent_password_container').hide();
                $('_accountcurrent_password').disable();
            });
        });
    //]]></script></td></tr>

    THANKs TO ALL HELPER !!

    Monday, May 13, 2019 10:45 PM

All replies

  • The rule of thumb is to find the specific element, ensure you have the right one then for instance to check a check box

    WebBrowser1.Document.GetElementById("account-send-pass").SetAttribute("checked", "true")

    To focus an element which accepts focus.

    WebBrowser1.Document.GetWlementById("someElement").Focus()

    The basic concept is shown below where the first two elements are assumed to be there while the last element is not assumed to be there. So keep in mind the code below is purely for showing the code flow.

    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' demo to set two elements to values in text boxes
            WebBrowser1.Document.GetElementById("firstName").SetAttribute("value", firstNameTextBox.Text)
            WebBrowser1.Document.GetElementById("lastName").SetAttribute("value", lastNameTextBox.Text)
    
            Dim radioEle =
                (
                    From ele In WebBrowser1.Document.GetElementsByTagName("INPUT").Cast(Of HtmlElement)()
                    Where ele.Id = "2" Select ele
                ).FirstOrDefault
    
            If radioEle IsNot Nothing Then
                radioEle.SetAttribute("checked", "true")
            End If
    
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, May 13, 2019 11:19 PM
  • Thank you Karen, 

    But the aim is to simulate a click on the textbox related to account[newpassword] element.

    It's the only way to have a second textbox account[current_password] shown as described in the javascipt extract.

    Its true i can check the send new password checkbox but with this function i can't set the password , the system does it and my tool is to initialize myself with password from my side.

    I tried the event wb.Document.GetElementById("account[new_password]").Focus() but no result.

    Thank you one more time for your help, 

    Tuesday, May 14, 2019 10:32 PM
  • This 

    wb.Document.GetElementById("account[new_password]").Focus()

    Will always fail because this "account[new_password]" is not an identifier, an identifier will look like this for a general example in a HTML page.

    <input type="text" id="name" name="name" required>

    So you would target it like

    wb.Document.GetElementById("name").Focus()

    So the basic code is as follows

    Dim result =
                (
                    From ele In WebBrowser1.Document.GetElementsByTagName("INPUT").Cast(Of HtmlElement)()
                    Where ele.Id = "name" Select ele
                ).FirstOrDefault

            If result IsNot Nothing Then
                result.Focus()
            End If


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, May 14, 2019 11:45 PM
  • HI Karen, 

    Thank you one more time for your help, 

    I read your answer and the reply you wrote :

    In the last version of VS i use impossible to use "From ele In wb.Document.GetElementsByTagName("INPUT").Cast(Of HtmlElement)()"

    Error is" Cast is not a member of HTML Collection" , same error with one other article i found wroten by your side.

    Maybe you use another version ?

    Wednesday, May 15, 2019 10:39 PM
  • I'm using Framework 4.7.2, VS2017, I can't even begin to image that VS2019 does not allow this code to work, Microsoft would not disallow this code from compiling.

    You might wonder why I'm not using VS2019, it's because I have never moved up to a new version of Visual Studio until the first patch is released as the initial version usually has quirks.

    Note also I have Option Strict On, Option Infer On.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 16, 2019 1:24 AM
  • i don't understand why i have this error

    Unable to send you the print screen but same error 

    cast is not a member of html collection

    Thank you Karen

    Sunday, May 19, 2019 12:14 AM
  • If you look at the documentation for GetElementByTagName it returns HtmlElementCollection. In my code sample I'm iterating the collection were each element in the collection is of type HtmlElement. 

    Documentation is for the most up to date Framework which is good for VS2019 and below.

    This is from the Microsoft documentation.

    Private Sub DisplayMetaDescription()
        If (WebBrowser1.Document IsNot Nothing) Then
            Dim Elems As HtmlElementCollection
            Dim WebOC As WebBrowser = WebBrowser1
    
            Elems = WebOC.Document.GetElementsByTagName("META")
    
            For Each elem As HtmlElement In Elems
                Dim NameStr As String = elem.GetAttribute("name")
    
                If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
                    If NameStr.ToLower().Equals("description") Then
                        Dim ContentStr As String = elem.GetAttribute("content")
                        MessageBox.Show("Document: " & WebOC.Url.ToString() &
                                        vbCrLf & "Description: " & ContentStr)
                    End If
                End If
            Next
        End If
    End Sub

    As you can see my code references the same as the code block above.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, May 19, 2019 9:47 AM