B4J Question Interact with HTML elements from B4j app?

ilan

Expert
Licensed User
Longtime User
hi

I would like to know if I can load a site into a webview and then fill an HTML input element with text by calling his id property?
or maybe simulate a button click in that site? (button element id is known)

thanx, ilan
 
Solution
You can try to inject JavaScript. I'm not sure whether it will work for all websites.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private WebView1 As WebView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    WebView1.LoadURL("https://www.b4x.com:51041/guessmynumber/index.html")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub JS(s As String)
    Dim joWV As JavaObject = WebView1
    joWV.RunMethodJO("getEngine"...

DonManfred

Expert
Licensed User
Longtime User
Maybe with Selenium?
 
Upvote 0

roumei

Active Member
Licensed User
You can try to inject JavaScript. I'm not sure whether it will work for all websites.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private WebView1 As WebView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    WebView1.LoadURL("https://www.b4x.com:51041/guessmynumber/index.html")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub JS(s As String)
    Dim joWV As JavaObject = WebView1
    joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(s))
End Sub

Private Sub Button1_Click
    JS($"document.getElementById("txtNumber").value="123";"$)
End Sub

Private Sub Button2_Click
    JS($"document.getElementById("btnGuess").click();"$)
End Sub
 

Attachments

  • TestHTML.zip
    8.7 KB · Views: 311
Upvote 3
Solution

ilan

Expert
Licensed User
Longtime User
Granted that I know ZERO... I know that some Erel's examples interact with web pages, using JQueryElement.
You can learn from those examples.
[WebApp] Chatroom...
[WebApp] Web Apps Overview
yes, but this will work if you are creating your own webapp and implement a WebSocket. what i want is to go to google.com and instead of writing inside the input field of the webpage i want to do it from a b4j app.


You can try to inject JavaScript. I'm not sure whether it will work for all websites.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private WebView1 As WebView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    WebView1.LoadURL("https://www.b4x.com:51041/guessmynumber/index.html")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub JS(s As String)
    Dim joWV As JavaObject = WebView1
    joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(s))
End Sub

Private Sub Button1_Click
    JS($"document.getElementById("txtNumber").value="123";"$)
End Sub

Private Sub Button2_Click
    JS($"document.getElementById("btnGuess").click();"$)
End Sub

thanx, will try that out :)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Maybe with Selenium?
thanx but none of the links are working so i cannot test it.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
yes, but this will work if you are creating your own webapp and implement a WebSocket. what i want is to go to google.com and instead of writing inside the input field of the webpage i want to do it from a b4j app.
But if you know the identifiers of the elements (buttons, input boxes, ...) as you wrote, maybe that's enough (I've never fiddled with this; I was just trying to give you a hint, but they will certainly give you better ones, as has just done @roumei :) ).
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
You can try to inject JavaScript. I'm not sure whether it will work for all websites.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private WebView1 As WebView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    WebView1.LoadURL("https://www.b4x.com:51041/guessmynumber/index.html")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub JS(s As String)
    Dim joWV As JavaObject = WebView1
    joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(s))
End Sub

Private Sub Button1_Click
    JS($"document.getElementById("txtNumber").value="123";"$)
End Sub

Private Sub Button2_Click
    JS($"document.getElementById("btnGuess").click();"$)
End Sub

you are a Guinness. thank you very much. your solution works perfectly. this is what i was looking for :) ???
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
But if you know the identifiers of the elements (buttons, input boxes, ...) as you wrote, maybe that's enough (I've never fiddled with this; I was just trying to give you a hint, but they will certainly give you better ones, as has just done @roumei :) ).

@roumei solution works as expected. now I would like to see if I can retrieve information from an HTML element. will make some checks.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
seeing can be of help
B4X
B4X:
Private Sub RunJavaScript (js As String) As ResumableSub
    #if B4A
    WebViewExtras1.executeJavascript(WebView1, $"B4A.CallSub('Process_HTML', true, ${js})"$)
    Wait For Process_Html(html As String)
    Return html
    #Else If B4J
    Return WebView1.As(JavaObject).RunMethodJO("getEngine", Null).RunMethod("executeScript", Array(js))
    #Else If B4i
    Dim sf As Object = WebView1.EvaluateJavaScript(js)
    Wait For (sf) WebView1_JSComplete (Success As Boolean, Result As String)
    Return Result
    #end if
End Sub
 
Upvote 1

ilan

Expert
Licensed User
Longtime User
seeing can be of help
B4X
B4X:
Private Sub RunJavaScript (js As String) As ResumableSub
    #if B4A
    WebViewExtras1.executeJavascript(WebView1, $"B4A.CallSub('Process_HTML', true, ${js})"$)
    Wait For Process_Html(html As String)
    Return html
    #Else If B4J
    Return WebView1.As(JavaObject).RunMethodJO("getEngine", Null).RunMethod("executeScript", Array(js))
    #Else If B4i
    Dim sf As Object = WebView1.EvaluateJavaScript(js)
    Wait For (sf) WebView1_JSComplete (Success As Boolean, Result As String)
    Return Result
    #end if
End Sub

thank you very much, i could not ask for a better solution :)
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
update sample @roumei
B4X:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private WebView1 As WebView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    WebView1.LoadURL("https://www.b4x.com:51041/guessmynumber/index.html")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.


Private Sub Button1_Click
    RunJavaScript($"document.getElementById("txtNumber").value="123";"$)
End Sub

Private Sub Button2_Click
    RunJavaScript($"document.getElementById("btnGuess").click();"$)
End Sub

Private Sub Button3_Click
    Wait For (RunJavaScript($"document.getElementById("txtNumber").value;"$)) Complete(Html As String)
    xui.MsgboxAsync(Html, "Get Field")
End Sub

Public Sub RunJavaScript (JS As String) As ResumableSub
    #if B4A
    WebViewExtras1.executeJavascript(WebView1, $"B4A.CallSub('Process_HTML', true, ${js})"$)
    Wait For Process_Html(html As String)
    Return html
    #Else If B4J
    Return WebView1.As(JavaObject).RunMethodJO("getEngine", Null).RunMethod("executeScript", Array(JS))
    #Else If B4i
    Dim sf As Object = WebView1.EvaluateJavaScript(js)
    Wait For (sf) WebView1_JSComplete (Success As Boolean, Result As String)
    Return Result
    #end if
End Sub

1639917762705.png
 
Upvote 0
Top