How to get Element of WebView by class name?

I am loading website for example 'http:/example.com' in WebView, Let this page contain a element

<a class="x" href="/test1">Click here</a>

How can I get this element 'x' and its value of href from WebView.

Upvotes: 0

Views: 3448

Answers (1)

MatPag
MatPag

Reputation: 44861

You need something to read HTML back in your Java code, like showed in this Answer: how to get html content from a webview?

Then when you have your HTML content, you need a parser to extract element (and data) you need. In past i've used JSOUP to navigate the HTML and it worked really well, you can find it here https://jsoup.org/.

You could extract the class names and href value with this: (only a concept)

Document yourPage = Jsoup.parse(htmlString);    
Element aElement = yourPage.select("path to a element").first();
Set<String> classNames = aElement.classNames();
String url = aElement.attr("href");

If you need help, you can read here a pretty nice intro-tutorial from JSOUP documentation

Upvotes: 2

Related Questions