Android Question How To Programmatically Click a Web Button??

air cover

Member
Licensed User
Longtime User
How To Programmatically Click a Web Button created by HTML that is being displayed in a WebView object by my app?

I have a simple app that browses web pages. They are displayed in WebView1. One of the html web pages displays a clickable button. It's part of the web page, not part of my app, but of course it shows up in my app because my app is displaying that web page.


A user could currently click this button by pressing it manually. Once clicked, my WebView1 window displays an updated html page.

However, what I'd LOVE to be able to do is to click that one button programmatically so that my users don't have to bother pressing that button.

Possible? Easy? Already done and my search just didn't find it?

Any help would be appreciated. Thanks!
 

walterf25

Expert
Licensed User
Longtime User
How To Programmatically Click a Web Button created by HTML that is being displayed in a WebView object by my app?

I have a simple app that browses web pages. They are displayed in WebView1. One of the html web pages displays a clickable button. It's part of the web page, not part of my app, but of course it shows up in my app because my app is displaying that web page.


A user could currently click this button by pressing it manually. Once clicked, my WebView1 window displays an updated html page.

However, what I'd LOVE to be able to do is to click that one button programmatically so that my users don't have to bother pressing that button.

Possible? Easy? Already done and my search just didn't find it?

Any help would be appreciated. Thanks!
Check this out!
https://www.b4x.com/android/forum/threads/webviewextras.12453/#content

cheers
Walter
 
Upvote 0

air cover

Member
Licensed User
Longtime User
I did get an alert javascript command to execute, but weirdly, even though it was called after the WebView1.LoadHtml, the MyWebViewExtras.executeJavascript(WebView1, "alert('Hello World!')") code gets executed first, before the WebView1 even tries to load the page.



You have to press the OK to close the javascript Alert before the web page loads.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
that's probably because the load is async, there might be a load successfull event where you could inject you javascript.
 
Upvote 0

air cover

Member
Licensed User
Longtime User
that's probably because the load is async, there might be a load successfull event where you could inject you javascript.
Of course, but I used the async httpclient method
B4X:
        HttpClient1.Initialize("http")
        hc.Initialize("hc")
        req.InitializeGet("http://www.abc.com")
        hc.Execute(req, 1)

Which calls the hc_ResponseSucces when the page is ready!

B4X:
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)         'We got connection and data !!
Dim tempstrat As String
Dim tempindex As Int
Dim req As HttpRequest
Dim Javascript As String

        
'newWin.document.getElementById('foo').click() javascript to click a web page button

   webhtmlstring = Response.GetString("UTF8")   'This holds the returned data

   If TaskId=1 Then  
           WebView1.LoadHtml(webhtmlstring)
   End If
             
   Response.Release   
   Javascript="alert('Hello World!')"
   MyWebViewExtras.executeJavascript(WebView1, Javascript)       '<<<<<< This gets executed BEFORE the web page is even allowed to try to load!
             
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you do it good for one part but bad for the other :)

you do another load when the load was successfull and taskid=1 so that's again async and the alert will happen before that completes.

so something is wrong there but it's hard to tell without seeing the rest of the code.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Upvote 0

walterf25

Expert
Licensed User
Longtime User
You should avoid using: Response.GetString("UTF8"), it has been deprecated, it won't work on SDK > 14
Holy hell, you're alive :eek::eek::eek:
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
You should avoid using: Response.GetString("UTF8"), it has been deprecated, it won't work on SDK > 14
Yeah why are you not using the HttpUtils2 library instead?
 
Upvote 0

air cover

Member
Licensed User
Longtime User
You should avoid using: Response.GetString("UTF8"), it has been deprecated, it won't work on SDK > 14
Right. OK, I swapped out httputils code for httputils2.

Same problem, though. How do you display the web page with only 1 load?

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
   
    If FirstTime = True Then
        job1.Initialize("Job1", Me)
       job1.Download("http://www.abc.com") '<<<<<<<<<<<<<<<<<< 1st Load <<<<<<<<<<<<<<<<<<<<<<
        activitytitle="1"
        Activity.Title=activitytitle
    End If

End Sub

Sub JobDone (Job As HttpJob)
   Dim Javascript As String
  
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      Select Job.JobName
         Case "Job1", "Job2"
             WebView1.LoadHtml(Job.GetString) '<<<<< 2nd Load! <<<<<<<<<<<<<<<<<<<
             Javascript="alert('Job1 or 2!')"
             MyWebViewExtras.executeJavascript(WebView1, Javascript)

         Case "Job3"
            Activity.Title="Job3"

      End Select

   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you are confusing things.

you don't need a file downloader to load a site into the webview.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
if the button has an id you could use it with this

B4X:
Sub Globals
Dim wv As WebView
Dim wve As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
wv.Initialize("wv")
wv.JavaScriptEnabled=True

Activity.AddView(wv,0,0,100%x,100%y)
wv.LoadUrl("http://www.google.com")
End Sub

Sub wv_PageFinished (Url As String)
wve.addWebChromeClient(wv,"")
wve.executeJavascript(wv,"alert('yo')")
End Sub
 
Upvote 0
Top