4

I am trying to create an Android app for a Website, Which is not mine. But is a search engine for Restaurants. They have no API to work with. And i want to heedlessly browse their website and put the search query in the HTML Form and Click the Submit Button. And then Parse the Results and Use it with my Application Code. After doing loads of research here, i am finally asking for it. Question 1, Question 2, Question 3 and many more that i have looked so far. So all i know so far is if i want to do the same on Google.com i would write:

myWebView.getSettings().setJavaScriptEnabled(true);

myWebView.loadUrl("http://www.google.com/");

myWebView.setWebViewClient(new WebViewClient() {

@Override
public void onPageFinished(WebView view, String url) {
       //Load HTML
            myWebView.loadUrl("javascript:document.getElementById('q') =" + "StackOverFlow" + "; document.getElementByName('btnK').click();");
        }
    });

In the above code i am trying to put the search term "StackOverFlow" and Click the Search Button. But its not working. Kindly Help me out in this code or either point me in the right direction.

Community
  • 1
  • 1

2 Answers2

2

It's been a while, but for the sake of letting others know, webviews no longer use loadUrl to run Javascript. Try using evaluateJavascript.

Since you've also mentioned headless browsing, I would recommend overriding shouldInterceptRequest in your client to redirect all unnecessary files (such as css, images, and perhaps js depending on the site) to a blank inputstream

Allan W
  • 2,791
  • 4
  • 23
  • 41
0
myWebView.loadUrl("http://www.google.com/");

after overriding onPageFinished method not before

M J
  • 174
  • 1
  • 12
  • You mean, without adding the webview to the visual tree (or whatever it is called, you know what I mean)? Is it efficient? The webview doesn't create visual elements in the background even though it is not part of the visual tree? – Damn Vegetables Aug 29 '21 at 12:30