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?
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?
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.
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
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
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