How can I, in an Applet, get the Window containing the Applet as an Window Object?
My goal is to make a screenshot of the content of a "Website".
I tried to make this with the Robot object:
Window window = ???
BufferedImage bufferedImage = new Robot().createScreenCapture(window.getBorder());
To get the border of the browser is the reason why I need this as a window object.
Or is they another possibility to do this?
To get the border of the browser is the reason why I need this as a window object.
Figure how to do it in JavaScript. Call the JavaScript from the applet.
This might not be the only way to do it, but it is arguably the best. It uses JS for the type of thing that JS is especially good for, interacting with the web page. Get it working and debugged in JS, then it is a relatively simple matter to call that function from the applet.
Related
Currently using selenium (in Java) for automated testing. When running the tests on a selenium grid, the window size changes to a window much smaller than my local (I have no control over the window size when doing a grid run). The smaller window width during the grid run causes the page objects to move in the HTML, changing their references (xpaths, css selectors, etc.). This causes the code to throw a bunch of null pointers. Any ideas on how to get around this? I need a solution that works in Chrome/Firefox/IE
Since I am unable to change the window size, the best solution I've come up with is to zoom out the browser when doing a grid run, moving the objects back into place. I'm using selenium 4.0.0-alpha-1 and tried the following approaches:
Actions class:
new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT)).perform();
Targeting the html tag with the Actions class:
new Actions(driver).sendKeys(driver.findElement(By.tagName("html")),
Keys.chord(Keys.CONTROL, Keys.SUBTRACT)).perform();
Targeting the html tag with the webdriver:
driver.findElement(By.tagName("html"))
.sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
Using javascript:
engine.javaScript.execute("document.body.style.zoom = '75%'");
engine.javaScript.execute("document.body.style.webkitTransform='scale(.75)'");
Using the Robot class:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_CONTROL);
--
Methods 1-3 don't work locally or on the grid.
Method 4 works on all browsers (used webkitTransform since its supported across all 3) but it zooms out the CSS and not the browser itself, so the objects remain in the same place despite being zoomed out.
Method 5 works across all browsers locally, but when running on the grid, it nothing happens because I believe the robot needs a GUI to interact with.
Most answers to this question are over 2 years old and don't seem to work anymore, would appreciate any help or if there is another solution to getting around the window sizing. Trying to get around creating separate object references specifically for the grid.
I have a Java application which displays results on a JPanel. The results are displayed using HTML by using a JLabel.
Now I want to let the user interact with the local application by allowing local methods to be called when the user clicks on a link in that HTML document.
Is this possible?
To answer you question, then, it is possible, however you cannot use a JLabel, you need to insert a JavaFX component, and then you can set your class as a window variable on the DOM, and thus your methods can be called from JavaScript.
Have a look at this answer on this question. It looks like they are doing exactly what you want.
Is there a way to copy text from a browser to my Java app ?
For example, at the left side of my screen I open a browser to point to a URL and shows the content of that page, it might be in a frame or CSS or simple html, on the right side of the screen I open a Java Swing application. I'm interested in certain parts of the browser window that shows some text, and I want my Java app [ without me doing anything ] to copy and paste the text into itself, can it be done ?
I know I can use JEditorPane or JTextPane and set it with an HTMLEditorKit, then load the text into the pane, but if the page uses Frames or some other complex ways, the text I get from the Pane is not what I see on the page, so I don't want to do it by loading the URL into my Java app, instead, I wonder if it can be done in the way I mentioned above ?
I think you're looking at the problem from the wrong angle. If what you want is to harvest a website, I suggest you have a look at the awesome library web-harvest. With a little Xpath wizardry you can get everything you want.
Doing what you describe would imply inter-process communication that seems like an overkill. There are more ways to download a web-page content than the browser.
You may try the following depending on your needs.
With java.awt.Robot you can either 1) Take an screenshot if what you neeed is the content ( without the text, just an image of the browser content ) or 2) Move your self into the browser and programatically press: CTRL-A + CTRL-C and return back to your swing app focus and programatically press: CTRL-V ( or CMD or whatever makes sense in your OS )
But again, this might or not work, depending on what you need.
I know a tool but i am not sure it meet your needs. Have you heard about selenium? http://seleniumhq.org/ It can replicate actions taken by the user in a browser and then manipulate them ussing java code. Have a look at the link it may be handy.
Using java.awt.Robot & a TextField will get the job done, not sure if there is any other way. Have robot press ctrl+a then ctrl+c, bring TextField into focus, and finally have robot press ctrl+v. Now from here you can create a button.setOnAction to save the TextField text into a string. Or you can use a change listener on the TextField setOnKeyReleased to do the same.
Hoping someone can point me in the right direction.
I'm quite new to Java and wanting to build a small app to practice etc.
Anyway I want to fill out a form on a site and grab the data. I have had a look at HtmlUnit and HtmlComponents and think I have most of the design covered.
Something I can't figure out is the site has a single captcha and I want my desktop app to popup a window where I would enter the captcha (manually, I'm not creating a bot).
Is this possible in Java?
It's possible and in fact JDownloader is doing it. It's open source, so you can take a look to its code.
As mentioned before JDownloader does this technique quite well. You mentioned the source is rather difficult to understand, so I'll explain the basics of what one would do.
First you would want to create your code that will fill in the form, once the form hits the captcha have it open up a frame that has a spot for an image (which will be the captcha image), a textfield, and a submit button. The submit button should have an action that will send the text from the textfield to the captcha text field in the form.
Summary:
1: Create you class that will fill in the form
2: Once it gets to the capthca make it open your 'captcha solving frame'
3: Fill in the text field and create an actionPerformed on the button that will send your string to the textfield in the form
4: Throw in some sort of catch in case you improperly fill in the captcha
On a side note, this would be easier and considerably less verbose if done with Groovy (or a scripting language) rather than Java.
I created a dialog box like this:
String response =
JOptionPane.showInputDialog(null,"message","title",JOptionPane.PLAIN_MESSAGE);
I'd like to keep it always on top of all windows.
Do you have any idea?
Thanks!
In fact, using Java, having a system modal dialog is not possible. The best you can have is a toolkit modal option pane. That's to say an option pane that stay in front of all Java windows.
This example explains how Java6 allows you to do that.
Maybe I don't get the question, but I quickly created desktop app with code you posted and it actually is modal ...