Android Question How To Programmatically Click a Web Button??

Discussion in 'Android Questions' started by air cover, Jun 29, 2015.

  1. air cover

    air cover Member Licensed 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!
     
  2. walterf25

    walterf25 Well-Known Member Licensed User

    Check this out!
    http://www.b4x.com/android/forum/threads/webviewextras.12453/#content

    cheers
    Walter
     
    DonManfred and Peter Simpson like this.
  3. air cover

    air cover Member Licensed User

    So with Webviewextras I can send javascript code that can click a web page button??
     
  4. sorex

    sorex Expert Licensed User

    or you could load the page that's behind the button instead.
     
  5. air cover

    air cover Member Licensed User

    I wish! But the button is doing some processing. It's got to be clicked either manually or programmatically.
     
  6. air cover

    air cover Member Licensed 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.
     
  7. sorex

    sorex Expert Licensed User

    that's probably because the load is async, there might be a load successfull event where you could inject you javascript.
     
  8. air cover

    air cover Member Licensed User

    Of course, but I used the async httpclient method
    Code:
    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!

    Code:
    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
     
  9. sorex

    sorex Expert Licensed 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.
     
    walterf25 likes this.
  10. air cover

    air cover Member Licensed User

    Is WebView1.LoadHtml really a 2nd load?
    Or webhtmlstring = Response.GetString("UTF8")?

    I get that req.InitializeGet("http://www.abc.com") is a load.
     
  11. walterf25

    walterf25 Well-Known Member Licensed User

    Yes WebView1.LoadHtml is a second load.
     
  12. NJDude

    NJDude Expert Licensed User

    You should avoid using: Response.GetString("UTF8"), it has been deprecated, it won't work on SDK > 14
     
  13. walterf25

    walterf25 Well-Known Member Licensed User

    Holy hell, you're alive :eek::eek::eek:
     
  14. NJDude

    NJDude Expert Licensed User

    Barely. :D
     
  15. walterf25

    walterf25 Well-Known Member Licensed User

    Yeah why are you not using the HttpUtils2 library instead?
     
  16. walterf25

    walterf25 Well-Known Member Licensed User

    Good to hear (read) you're hanging in there bud!
     
  17. air cover

    air cover Member Licensed User

    Right. OK, I swapped out httputils code for httputils2.

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

    Code:
    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
     
  18. sorex

    sorex Expert Licensed User

    you are confusing things.

    you don't need a file downloader to load a site into the webview.
     
  19. air cover

    air cover Member Licensed User

    You'd think, but no...I do. The website will eventually be modified in code before it is displayed.
     
  20. sorex

    sorex Expert Licensed User

    if the button has an id you could use it with this

    Code:
    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
     
    air cover likes this.
Loading...