I have a Java 7 program (using WebStart technology, for Windows 7/8 computers only).
I need to add a function so that my program clicks a button on a page with known URL (https).
Some people suggest WebKit SWT, but I went to their site and they say that the project was discontinued. (http://www.genuitec.com/about/labs.html)
Other people say that JxBrowser is the only option but it looks like it's over $1,300 which is crazy. (http://www.teamdev.com/jxbrowser/onlinedemo/)
I'm looking for something simple, free, lightweight, and able to open HTTPS link, parse HTML, access a button through DOM and click it. Perhaps some JavaScript too, in case there are JS handlers.
Thanks for your help.
You may be looking for HtmlUnit -- a "GUI-Less browser for Java programs".
Here's a sample code that opens google.com, searches for "htmlunit" using the form and prints the number of results.
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
public class HtmlUnitFormExample {
public static void main(String[] args) throws Exception {
WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("http://www.google.com");
HtmlInput searchBox = page.getElementByName("q");
searchBox.setValueAttribute("htmlunit");
HtmlSubmitInput googleSearchSubmitButton =
page.getElementByName("btnG"); // sometimes it's "btnK"
page=googleSearchSubmitButton.click();
HtmlDivision resultStatsDiv =
page.getFirstByXPath("//div[#id='resultStats']");
System.out.println(resultStatsDiv.asText()); // About 309,000 results
webClient.closeAllWindows();
}
}
Other options are:
Selenium: Will open a browser like Firefox and operate it.
Watij: Also will open a browser, but in its own window.
Jsoup: Good parser. No JavaScript, though.
Your question is kind of difficult to understand what you want. If you have a webstart app and want to open a link in the browser, you can use the java.awt.Desktop.getDesktop().browse(URI) method.
public void openLinkInBrowser(ActionEvent event){
try {
URI uri = new URI(WEB_ADDRESS);
java.awt.Desktop.getDesktop().browse(uri);
} catch (URISyntaxException | IOException e) {
//System.out.println("THROW::: make sure we handle browser error");
e.printStackTrace();
}
}
Related
I'm trying to set up my first very simple UX test using Vaadin TestBench. To avoid the headache of downloading drivers and setting System.properties or PATH values, I'm also using the WebDriverManager library.
To make it a little trickier, our login page is a JSP that we will need to open and authenticate before being able to test the Vaadin application.
Here is the simple test I've been trying:
public class LoginIT extends TestBenchTestCase {
private static final String URL="http://localhost:8080/";
#Before
public void setup() throws Exception {
ChromeDriverManager.getInstance().setup();
setDriver(new ChromeDriver());
}
#After
public void tearDown() throws Exception {
if (getDriver() != null) {
getDriver().quit();
}
}
#Test
public void testLogin_success() {
getDriver().get(URL);
Assert.equals(URL, getDriver().getCurrentUrl());
WebElement usernameField = driver.findElement(By.name("username"));
}
}
The simple test above will pass on the currentUrl assertion. However, it fails to find the element.
I think I have two issues here.
The browser doesn't open/navigate to the URL. Chrome doesn't open a new tab/page when running the test if Chrome is already open. Alternatively, if I allow it to launch the browser it does so without opening a page (on Mac OSX) so I never can visually confirm it navigates to the URL.
I've tried with Firefox, which apparently has a lot of issues with Selenium, and PhantomJS, which has issues with a missing .lib file in the latest binary. With the WebDriverManager, I downgraded to PhantomJS 2.0 but it times out waiting for http://localhost:29436/status.
If it does "successfully" navigate to the URL, as Chrome says it does, it isn't able to find the element. This might be due to number 1?
If TestBench isn't able to handle the JSP login, then it will be useless for my application. Any help is greatly appreciated. What could I be doing incorrectly that is causing my issues?
Created a simple test example for this issue
https://github.com/rogozinds/testbenchexample
What version of Testbench you are using?
Can you try to run it without using ChromeDriverManager, just download
chromedriver https://sites.google.com/a/chromium.org/chromedriver/downloads
and add it to your system PATH.
getDriver().get(URL) - should at least open a new Chrome Window and navigate to URL. But as I understood that's not happening?
P.S I've tried your example with Testbench 5 without ChromeDriverManager and simple index.html file it works.
I am creating a game and I made a Launcher. I have seen on other games made out of Java (Like MineCraft) have a webpage on the launcher. I was woundering how to put a webpage on a Java Swing GUI panel. I would also like to know how to open their browser up to a link with a button.
Thanks,
Blockquote
To open a url in the system's web browser you can use java.awt.Desktop.browse(URI). This allows you to keep your Java code platform independent, and even allows you to check to see if an operation is supported before trying to use it.
To load a web page within Java, I've had some success using the JavaFX WebView.
import java.awt.Desktop;
import java.net.URI;
class URLBrowsing
{
public static void main(String args[])
{
try
{
// Create Desktop object
Desktop d=Desktop.getDesktop();
// Browse a URL, for example www.facebook.com
d.browse(new URI("http://www.facebook.com"));
// This open facebook.com in your default browser.
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
I need to parse page, everything is ok except some elements on page are loaded dynamically. I used jsoup for static elements, then when I realized that I really need dynamic elements I tried javafx. I read a lot of answeres on stackoverflow and there were many recommendations to use javafx WebEngine. So I ended with this code.
#Override
public void start(Stage primaryStage) {
WebView webview = new WebView();
final WebEngine webengine = webview.getEngine();
webengine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
Document doc = webengine.getDocument();
//Serialize DOM
OutputFormat format = new OutputFormat (doc);
// as a String
StringWriter stringOut = new StringWriter ();
XMLSerializer serial = new XMLSerializer (stringOut, format);
try {
serial.serialize(doc);
} catch (IOException e) {
e.printStackTrace();
}
// Display the XML
System.out.println(stringOut.toString());
}
}
});
webengine.load("http://detail.tmall.com/item.htm?spm=a220o.1000855.0.0.PZSbaQ&id=19378327658");
primaryStage.setScene(new Scene(webview, 800, 800));
primaryStage.show();
}
I made string from org.w3c.dom.Document and printed it. But it was useless too. primaryStage.show() showed me fully loaded page (with element I need rendered on page), but there was no element I need in html code (in output).
This is the third day I'm working on that issue, of course lack of experience is my main problem, nevertheless I have to say: I'm stuck. This is my first java project after reading java complete reference. I make it to get some real-world experience (and for fun). I want to make parser of chinese "ebay".
Here is the problem and my test cases:
http://detail.tmall.com/item.htm?spm=a220o.1000855.0.0.PZSbaQ&id=19378327658
need to get dynamically loaded discount "129.00"
http://item.taobao.com/item.htm?spm=a230r.1.14.67.MNq30d&id=22794120348
need "15.20"
As you can see, if you view this pages with browser at first you see original price and after a second or so - discount.
Is it even possible to get this dynamic discounts from html page? Other elements I need to parse are static. What to try next: another library to render html with javascript or maybe smth else? I really need some advice, don't want to give up.
DOM model returned after Worker.State.SUCCEEDED shoulb be already processed by javascript.
Your code worked for me with tested with FX 7u40 and 8.0 dev. I see next output in the log:
<DIV id="J_PromoBox"><EM class="tb-promo-price-type">夏季新品</EM><EM class="tm-yen">¥</EM>
<STRONG class="J_CurPrice">129.00</STRONG></DIV>
which is dynamically loaded box with data (129.00) you looked for.
You may want to upgrade your JDK to 7u40 or revisit your log parsing algorithm.
It sounds like you want the rendered DOM from a dynamic page after the Javascript on the page has finished modifying the original HTML. This would not be easy to do in Java as you would need to implement browser-like functionality with an embedded Javascript engine. If you only care about reading a web page from Java, you might want to look into Selenium since it takes control of a browser and allows you to pull the rendered HTML into Java.
This answer might also help:
Render JavaScript and HTML in (any) Java Program (Access rendered DOM Tree)?
I have a storage box admin page that's supposed to open with Java Web Start. However, on all browsers on my MacBook, this doesn't happen and instead I just get a html page saved with contents: "v6.3.1a Web Tools 10.1.18.222 ".
Looking at the javascript code of the page, I see it is trying to detect if correct Java Web Start is installed:
function webstartVersionCheck(versionString) {
// Mozilla may not recognize new plugins without this refresh
navigator.plugins.refresh(true);
// First, determine if Web Start is available
if **(navigator.mimeTypes['application/x-java-jnlp-file'])** {
// Next, check for appropriate version family
for (var i = 0; i < navigator.mimeTypes.length; ++i) {
pluginType = navigator.mimeTypes[i].type;
if (pluginType == "application/x-java-applet;version=" + versionString) {
return true;
}
}
}
return false;
}
Which is called here:
function writeMozillaData(page) {
versionCheck = webstartVersionCheck("1.5");
if (!versionCheck) {
var pluginPage = "http://jdl.sun.com/webapps/getjava/BrowserRedirect?locale=en&host=java.com";
document.write("The version of Java plugin needed to run the application is not installed. The page from where the plugin can be downloaded will be opened in a new window. If not, please click here: Download correct Java version.");
window.open(pluginPage, "needdownload");
} else {
window.location = page;
}
}
I put in an alert in the mimeTypes and notice that there is no mimeType of 'application/x-java-jnlp-file' which shows up in navigator.
Questions:
Is this what is causing the browser to interpret the content as just text/html and save the html?
How can I force the launch of Java Web Start here?
I do have firefox settings indicating that jnlp get handled by Java Web Start application, hence I suspect the browser is not interpreting the page as jnlp at all to begin with.
..there is no mimeType of application/x-java-jnlp-file which shows up in navigator.
Is this what is causing the browser to interpret the content as just text/html and save the html?
Almost certainly yes. Fix the content-type.
Is there a simple way to open a web page within a GUI's JPanel?
If not, how do you open a web page with the computer's default web browser?
I am hoping for something that I can do with under 20 lines of code, and at most would need to create one class. No reason for 20 though, just hoping for little code...
I am planning to open a guide to go with a game. The guide is online and has multiple pages, but the pages link to each other, so I am hoping I only have to call one URL with my code.
Opening a web page with the default web browser is easy:
java.awt.Desktop.getDesktop().browse(theURI);
Embedding a browser is not so easy. JEditorPane has some HTML ability (if I remember my limited Swing-knowledge correctly), but it's very limited and not suiteable for a general-purpose browser.
There are two standard ways that I know of:
The standard JEditorPane component
Desktop.getDesktop().browse(URI) to open the user's default browser (Java 6 or later)
Soon, there will also be a third:
The JWebPane component, which apparently has not yet been released
JEditorPane is very bare-bones; it doesn't handle CSS or JavaScript, and you even have to handle hyperlinks yourself. But you can embed it into your application more seamlessly than launching FireFox would be.
Here's a sample of how to use hyperlinks (assuming your documents don't use frames):
// ... initialize myEditorPane
myEditorPane.setEditable(false); // to allow it to generate HyperlinkEvents
myEditorPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
myEditorPane.setToolTipText(e.getDescription());
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
myEditorPane.setToolTipText(null);
} else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
myEditorPane.setPage(e.getURL());
} catch (IOException ex) {
// handle error
ex.printStackTrace();
}
}
}
});
If you're developing an applet, you can use AppletContext.showDocument. That would be a one-liner:
getAppletContext().showDocument("http://example.com", "_blank");
If you're developing a desktop application, you might try Bare Bones Browser Launch.
haven't tried it at all, but the cobra HTML parser and viewer from the lobo browser, a browser writen in pure java, may be what you're after. They provide sample code to set up an online html viewer:
import javax.swing.*;
import org.lobobrowser.html.gui.*;
import org.lobobrowser.html.test.*;
public class BareMinimumTest {
public static void main(String[] args) throws Exception {
JFrame window = new JFrame();
HtmlPanel panel = new HtmlPanel();
window.getContentPane().add(panel);
window.setSize(600, 400);
window.setVisible(true);
new SimpleHtmlRendererContext(panel, new SimpleUserAgentContext())
.navigate("http://lobobrowser.org/browser/home.jsp");
}
}
I don't know if such a built-in exists, but I would use the Runtime class's exec with iexplore.exe or firefox.exe as an argument.
Please try below code :
import java.awt.Desktop;
import java.net.URI;
import java.net.URISyntaxExaception;
//below is the code
//whatvever the url is it has to have https://
Desktop d = Desktop.getDesktop();
try {
d.browse(new URI("http://google.com"));
} catch (IOException | URISyntaxException e2) {
e2.printStackTrace();
}