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!
 

sorex

Expert
Licensed User
Longtime User
using

B4X:
wve.executeJavascript(wv,"document.getElementById('lst-ib').value='test';document.forms(0).submit()")

it opens google, types "test" in the search and do the actual search for you.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
but as I said before you could bypass the first step in 99% of the cases that you don't need all this hassle.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
can you share the link or send it by private message?

then I'll see if some trickery can be used to do it the easy way.
 
Upvote 0

air cover

Member
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

That's nice, clean code, thanks. It does display your google.com page and then say "yo" (then crashes).

Replacing the "yo" javascript with
B4X:
wve.executeJavascript(wv,"document.getElementById('lst-ib').value='B4A';document.forms(0).submit()")
displays "B4A" in the google text input box, but doesn't search (possibly suggesting that the search button wasn't programmatically clicked).
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
indeed, the app closes here aswell but it doesn't give an error in the logs.
 
Upvote 0

air cover

Member
Licensed User
Longtime User
OK, this is cool. B4A has an asynch subroutine that lets you code after your web page loads into the webview tool! Well done, guys!

B4X:
 Sub WebView1_PageFinished (Url As String)
    MyWebViewExtras.executeJavascript(WebView1,"alert('yo')")
End Sub
 
Upvote 0

air cover

Member
Licensed User
Longtime User
Thanks...but weirdly enough, in Release mode the web page is loading, but the "B4A" text isn't even being entered into the input box...whereas in debug mode it does get entered, just not searched.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
well, I said it before. you can bypass that misery if you know how to transmit the data without the use of the webview.
 
Upvote 0
Top