steven
steven

Reputation: 199

How do I get all CSS properties applied on any element in a webpage using JAVA?

I have a html page, with few CSS files, and JS. I want to list all CSS properties which are applied on a given #id in the html. This I want to do using Java. I can do it by writing code from scratch, but are there any Libraries which can do it easily?

Upvotes: 0

Views: 824

Answers (3)

user1594257
user1594257

Reputation: 495

It sounds like you want to use something like Selenium because you want all of the applied CSS properties and not the css class names.

http://www.seleniumeasy.com/selenium-tutorials/how-to-get-css-values-using-webdriver

Upvotes: 0

Eugen
Eugen

Reputation: 917

You can try jsoup. It provides a very convenient API for working with HTML. For get CSS properties try:

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
Elements idElement= doc.select("#idElement");
Attributes attrs = idElement.attributes​();
attrs.asList​().stream().forEach( e -> {
    System.out.println(e.getKey​() + " :" + e.getValue​());
});
Set<String> = idElement.classNames​();

Upvotes: 2

Isaac
Isaac

Reputation: 804

I presume you mean you want to use a Java library, to scrape the content off of your web page. A good library to use would be Jsoup. The linked example shows you how to use the libraries syntax selector which sounds like what you want to do! More specifically you'll want to use the selector for #ID which targets the name of the ID you want!

Document doc = Jsoup.connect("http://example.com").get();
doc.select("#id").forEach(System.out::println);

Would print all of the ID div out. You can then also save these to a set if you want to.

You would first need to install Jsoup. Do so by clicking and following instructions here Download

Upvotes: 0

Related Questions