简体   繁体   中英

Java Killing Popup Process

I'm now writing a simple program to simulate real user surfing web. However, if there is any popup or program pop up on the screen, I need to either minimize or close it to prevent it from interacting with the wrong program. So I have to monitor the running process or detect for new process? I tried using Javascript but it requires ActiveX in IE and quite problematic. Can someone kindly advise me on how to start on writing that part of the code? I googled for a bit and it says those process window.opener.close() or Process.Destroy() commands. Thanks. Below is a simple draft of my code.

package javaapplication1;
import java.util.Random;

public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        try
        {
            Random rand = new Random();
            int n = 1 + rand.nextInt(3);
            if (n == 1)
            {
                String myURL = "www.facebook.com";
                java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
                java.net.URI myNewLocation = new java.net.URI(myURL);
                myNewBrowserDesktop.browse(myNewLocation);
            }
            if (n == 2)
            {
                String myURL = "www.google.com";
                java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
                java.net.URI myNewLocation = new java.net.URI(myURL);
                myNewBrowserDesktop.browse(myNewLocation);
            }
            if (n == 3)
            {
                String myURL = "www.yahoo.com";
                java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
                java.net.URI myNewLocation = new java.net.URI(myURL);
                myNewBrowserDesktop.browse(myNewLocation);
            }
        }
        catch(Exception e)
        {
        }
    }
}

This cannot be done. Desktop.browse Launches the default browser to display the URI . The content of the document (html) that browser displays may cause the browser to open a new window or do anything else that a browser might do when you browse that URI manually in your browser.

In most cases, the browser will not create a new process. Irrespective of whether it creates a new process or only a window, it will not provide any notification to your calling program; so your calling program has no way of controlling those windows.

You may be able to do something like this by using an embedded browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM