I need to access HTML elements from my Java program based on the id or className of the element (like getElementByID or getElementsByClassName). I also need to be able to click a few buttons on the page.
Main Points:
I am creating a desktop application. It is not a web app.
I need a browserless solution
My code needs to auto fill a form and submit it without opening a page.
Are there any libraries out there that could suit my needs, or could I achieve this in plain Java code? If my question isn't clear please let me know and I will try to explain it in a better way. Thank you.
Selenium is a library used for scraping html elements using java. It is often used for testing. There is another library called Vaadin. It is used for doing front-end work on the back-end using java. It is similar to Java Swing but used on the web. Hope this helps!
Related
Good evening, I'm working on a project with a team, we have to make a browser without using JEditorPane or any other class that reads HTML.
How can we do that? Do we need to make a new class that does what JEditorPane does? Can I find somewhere JEditorPane's code? Thanks!
Well, this is an answer:
If you need to display web content without using any pre-existing engine (such JEditorPanel or a ChromeBind), you need to read the HTML as a XML file and construct your native View based on it (without CSS and JS this is a fairly easy task) by constructing the screen based on a one-to-one equivalent of a HTML tag to a Java JComponent.
Modern Web Browsers are pretty complicated, so there are a lot of different pieces that come together to display a web page. In order to build a browser, you need to first understand what a browser is. For that, I recommend reading this tutorial.
Once you have an understanding of how a browser actually works you need to determine which pieces you can reuse and which pieces you have to write from scratch. Do you have to write the entire rendering engine? Good luck! Can you use an existing engine like Gecko or Webkit? Or maybe you can get a little closer to done and use the java port of Webkit?
Once you have a better understanding of the question come back and ask more direct questions when you get stuck at a specific piece. As it is, your first step is to gain an understanding of the problem you are trying to solve.
I work for a start-up, where we have a requirement to automatically navigate to a given web application and find out information about all the objects contained within a page (inclusive of any iframes inside). We are supposed to code this module in Java.
So, I used Selenium WebDriver and was successful. However, due to some reasons, we've been asked not to use Selenium, but rather Core Java to do this.
So here's my question. Let's say I want to open "http://www.google.co.in" on my Firefox browser, and I have to get the attribute values for the Search Textbox, Search button and I'm feeling Lucky button. I have to do this using Java. Where do I start?
I had an idea, which was to actually navigate to a page, read its HTML source and build an xpath query to find each element and get its attributes. But how do I accomplish this navigation using Java (or jQuery as well, if that's possible)?
It may sound as if I'm trying to build an automation tool from the scratch, but I'm just considering all possibilities.
Please help.
If you have loaded the HTML content of the page into a single string variable, you can use standard Java string mechanisms to find contents of the HTML page in your string.
This might help http://www.javaworld.com/article/2077567/core-java/java-tip-66--control-browsers-from-your-java-application.html
Don't know why you want to do in Java instead of Selenium. Selenium will be the best tool for this job, you should convince your team instead.
I would like to know the best way and what it would require(languages/libraries) to generate Java GUI code from an HTML page with embedded Javascript code. It would emulate the HTML objects(like divs/tables) and their interactions. I don't need to emulate any complicated Javascript libraries, but just get basic functionality like handling mouse clicks/movements and key presses which Java can easily support.
I was thinking of representing all of the HTML objects using absolute positioning in Java without having to generate code for layout managers.
EDIT:
Regarding seand's comment: I meant that I don't need to convert code written in JQuery or other Javascript libraries into Java GUI code. I only want to deal with simple Javascript.
I was searching as well and it appears that nothing seems really meant to do that.
However one option is to embed a browser into your app using something like:
http://djproject.sourceforge.net/ns/
http://www.eclipse.org/articles/Article-SWT-browser-widget/browser.html
And then used it to load your HTML pages.
I am trying to build a search engine using java and the lucene API as part of a project. For the last step, we plan to build a web UI (a local host would do) for the same. Are there UI softwares/plugins for eclipse which will allow me to call the functions present in the java classes?
Essentially I would want to have a search box and a search key, pressing which will throw up the search results(which is computed from the java program). javascript cannot call java code I understand. So using that is eliminated?
Any suggestions on what to use will be greatly appreciated. I have pretty poor knowledge in front end design!
Cheers!
AB
If all you have is a simple screen with a entry field and a button and you simply want to return an html table. I would go with a servlet and two jsps. Your servlet can call your search engine and then have the jsp format the data into the table. If you do not know web apis this is probably the easiest entry.
I think, If your using JAVA, that you should look into JSF.
It's a rather easy to maintain and work with library for just the uses you describe.
I recommend these tutorials to get you started: http://www.coreservlets.com/JSF-Tutorial/jsf2/#Tutorial-Intro
There are lots of options to achieve this.
you can create web-ui using jsp.
I have also created same type of project using Lucene, here i have used spring mvc.i have provided all the back-end process as REST api which any web-ui can use.
Please do not look into JSF; it is an overengineered pile for your task.
Sure you can call your java code from javascript, you can make it really simple with something like DWR.
However, for your project I would suggest GWT as then you only deal with Java and it will generate javascript, html and css for you.
For your project you dont really need an "enterprise" level framework like spring or a fullstack JavaEE, you could keep it real oldschool with only JSPs and html/javascript. However thats a bit too flaky for my taste, so go with GWT.
With GWT you basically set it up, define your module, entrance point (look at the hello world), and then you add a layout to your page like something to place the searchbox into and the resultbox to. Then you call your other Java code and classes from there like you normally would.
I would suggest you to use GWT in your application because GWT enables you to call java methods and it will also convert Javascript and css for your Java modules after GWT compile.
GWT reference :- http://code.google.com/webtoolkit/gettingstarted.html
If you're going to use GWT, you could aslo check Vaadin.
Creating a search UI is really simple, and the tutorial show a criteria /result table application taht could be adapted.
I have Java code of the algorithm that has numeric values as its input and output. The GUI is implemented as Swing in Eclipse. Now I would like to tranfer to the web environment and create the web-tool using JavaScript or sth like this. Is there any way to use the Java code of my algorithm without rewriting it?
if your logic and goi all together at the same class you need to remove all the GUI part and stay with all the logic. after you have logic class you can use what ever goi you want(swing, html, jsf....)
Another alternative is to skip the HTML part by hand and just create your HTML app in Java. No need for separate servlets serving Java validation to your HTML inputs. The open source framework Vaadin that we've been working on since 2001 and it has an almost 100% similar API to Swing could be an alternative for this for you. And it would hopefully save you a lot of time.
As a swing developer the most relevant starting point for you would be: https://vaadin.com/swing
You can look up in to servlets and if you do it in netbeans then will be much easier for you.