Java Robot: how to click on the first text that has been amplified (using the search function) in the browser?

I am creating a java robot that after searching for a keyword in the browser, the mouse will move directly to that instance of that keyword (which is highlighted).

My problem: I don't know the position (x, y) in advance, I can't use mouseMove (x, y).

Thanks in advance for your help.

Please see the file attached.


source to share

1 answer

The only way to do this, assuming you don't want to rewrite your browser from scratch in Java, is to use Robot.createScreenCapture () to create a BufferedImage, then iterate over the pixels of the image to find the highlight color.

The problem is that other things in the browser window can be this color, in which case you need to be smarter to go to the right place.

Shameless Self-Promotion: A while ago I created a program that did just that to automatically click the "I'm Still Listening" button on Pandora. Pandora has since changed the look of its button so that it no longer works, but the code is still available if you want to test it:

Edit: This also assumes that the highlight color is always the same. You might want to test this assumption before diving.



All Articles