B4J Question WebView Manipulate

astronald

Active Member
Licensed User
Longtime User
can i manipulate web control as vb6 ?

Webview.Document.GetElementByID("txtname").value = "nameuser"
Webview.Document.GetElementByID("txtpass").value = "pass"
 

Tayfur

Well-Known Member
Licensed User
Longtime User
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I attach a very early wrap of webengine and a large part of org.w3c.dom.html Documentation can be found here: https://docs.oracle.com/javase/9/docs/api/org/w3c/dom/html/package-summary.html

Although the documentation is for Java 9, I am using it on Java 8, everything I've tried so far is supported.

Caveat emptor:
This is very much a work in progress (not for the feint hearted), it is too early for me to releases it as a finished library but you are welcome to try it. The syntax is different than vb6 (it appears) let me know if you find any problems.

I think your statement would be : mDocument.GetElementById("txtName").SetAttribute("value","nameuser")

You can compile it to a library in the usual way.

As in the Main module, you need to add a worker listener to know when the page has loaded, then you can manipulate the Page Document.

If you try it Let me know how you get on with it.


Update: 2018/10/01 -

  • Added HTMLInputElement
  • Changed Document.GetElementByID to return uninitialized element if not found.
  • 2nd Update
    • LoadProgress_Event sub tries to determine if the page is being redirected.

3
 

Attachments

  • WebEngine.zip
    42.2 KB · Views: 276
Last edited:
Upvote 0

astronald

Active Member
Licensed User
Longtime User
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have you put the code in the loadprogress_event sub inside the test for SUCCEEDED?
 
Upvote 0

astronald

Active Member
Licensed User
Longtime User
This is my code
B4X:
Private Sub LoadProgress_Event (NewState As String)
   
    If NewState  = "SUCCEEDED" Then
        Log("Page LOADED")
        mDocument = WebE.GetDocument
       
'        Dim Div As Element = mDocument.CreateElement("div")
'        mDocument.AsHTMLDocument.GetBody.AsHTMLNode.AppendChild(Div)
'        Dim Txt As HTMLText = mDocument.CreateTextNode("Test")
'        Div.AsHTMLNode.AppendChild(Txt)
'        Dim Txt As HTMLText = mDocument.CreateTextNode(" Test2")
'        Div.AsHTMLNode.AppendChild(Txt)
        mDocument.GetElementById("vistaLogin:frmLogin:txtNit").SetAttribute("value","900755449")


    End If
End Sub

this get element but when set attribute ccrash,
i try ucase Value and value but not yet
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Is the elementID Correct, including the case?
 
Upvote 0

astronald

Active Member
Licensed User
Longtime User
I log (NewState) and detect 3 "SUCCEEDED"
de last is for load page complete
B4X:
Waiting for debugger to connect...
Program started.
SCHEDULED
RUNNING
SUCCEEDED
SCHEDULED
RUNNING
SUCCEEDED

Change CODE ADD Button, on the button click:
B4X:
Sub Button1_Click
    mDocument = WebE.GetDocument
    mDocument.GetElementById("vistaLogin:frmLogin:txtNit").SetAttribute("value","900755449")
End Sub

but this does not work or crash.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I tried it on the B4x web page search box and it worked, on the google search box it didn't as it uses a function to parse the input.

Is the target input box on the web page a standard input box?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
"vistaLogin:frmLogin:txtNit" is a pretty odd format for an id. It is usually simpler than that.
 
Upvote 0

astronald

Active Member
Licensed User
Longtime User
this works properly
B4X:
    Dim joWV As JavaObject = WebView1
    joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript",Array As String("document.getElementById('vistaLogin:frmLogin:txtNit').value='900755449'"))

but i want use Document Class

Thank stevel
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
OK it needed the HTMLInputElement class wrapper which is now included in the download, your example is in the main module which works.

Download it again and let me know.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It appears that the page was redirected and the element didn't exist on the first page. Which may be why it failed in the first place.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Last update for today, the LoadProgress_Event tries to determine if the page is being redirected and waits for the redirected page before continuing (for 1 redirection only)

Updated to post 3
 
Upvote 0

astronald

Active Member
Licensed User
Longtime User
Last update for today, the LoadProgress_Event tries to determine if the page is being redirected and waits for the redirected page before continuing (for 1 redirection only)

Updated to post 3
wow this is great, i solve using webview_pagefinished .


I have try this but does not work:
B4X:
    Elemento = mDocument.GetElementById("vistaLogin:frmLogin:_id18")
    If Elemento.IsInitialized Then Elemento.AsHTMLInputElement.Click


how i can simulate button click.

thanks again.
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The button does not have an id, so you have to get the element by name, you also need to be able to access the HTMLinputElement Method click from HTML Node as that what is returned by HTMLDocument,getelementsByName.

Add this sub to the HTMLNode class :

B4X:
'Returns the Node As an HTMLInputElement Object
Public Sub AsHTMLInputElement As HTMLInputElement
    Dim E As HTMLInputElement
    E.Initialize
    E.SetObject(TJO)
    Return E
End Sub

Then change your code to :

B4X:
        Dim Elemento As HTMLNode = mDocument.AsHTMLDocument.GetElementsByName("vistaLogin:frmLogin:_id18").Item(0)
        If Elemento.IsInitialized Then Elemento.AsHTMLInputElement.Click

If the input is incomplete it will just go round and round reloading the page, so you will need to put some kind of count or trapping in the code just in case.
 
Upvote 0
Top