3

我正在开发一个使用 Runtime 类的 Java Selenium 程序。有多个打开的 Internet Explorer 窗口实例。
我只需要“带来前面”一个特定的窗口,硒套件就可以正常运行。

我已经使用 tasklist 命令检索了特定的 iexplore 实例。现在我有了进程的PID。

Process p = Runtime.getRuntime().exec("tasklist /FI \"WindowTitle eq Google\"");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
String line = null;  
int count=1;
while ((line = in.readLine()) != null) {
    System.out.println(count + " " + line);
    count++;
}

如何使用其PID通过运行时命令行将进程置于最前面(使其成为活动窗口)?

编辑:
有没有办法使用应用程序的 PID 从命令提示符切换到应用程序窗口?

4

2 回答 2

1

要从命令提示符切换到应用程序窗口,您可以使用 Windows Script Host 的 AppActivate 功能。它接受 ProcessID 或窗口的标题作为其参数。这是一个简单的脚本:

set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate Wscript.Arguments(0)

然后使用调用脚本cscript AppActivate.vbs 1234

于 2012-12-29T21:11:48.203 回答
0

我试图将上述建议应用到完整的解决方案中

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.commons.io.FileUtils.writeStringToFile;
import static org.apache.commons.lang3.math.NumberUtils.toInt;

public class WindowSwither {
    private static final Pattern TASKLIST_CSV_PATTERN = Pattern.compile("\".*?\",\"(\\d+)\".*");
    private int pidActive = 0;
    private final List<Integer> pidList = new ArrayList<>();

    public WindowSwither(String exeName) throws IOException {
        Process p = Runtime.getRuntime().exec("tasklist /FI \"ImageName eq " + exeName + ".exe\" /FO CSV");
        for (String line : showProcessOut(p)) {
            Matcher m = TASKLIST_CSV_PATTERN.matcher(line);
            if (m.find()) {
                pidList.add(toInt(m.group(1)));
            }
        }
    }

    public void next() throws IOException {
        if (pidList.size() == 0) {
            System.out.println("List empty! Ignored.");
            return;
        }

        if (pidActive == 0) {
            pidActive = pidList.get(0);
            System.out.println("[0] " + pidActive);
        } else {
            Iterator<Integer> it = pidList.iterator();
            while (it.hasNext()) {
                Integer pid = it.next();
                if (pid == pidActive) {
                    break;
                }
            }
            if (it.hasNext()) {
                pidActive = it.next();
                System.out.println("[+] " + pidActive);
            } else {
                pidActive = pidList.get(0);
                System.out.println("[0] " + pidActive);
            }
        }
        activate(pidActive);
    }

    private static void activate(int pid) throws IOException {
        String pathname = System.getProperty("java.io.tmpdir") + "WindowSwintcherAppActivate.vbs";
        File file = new File(pathname);
        if (!file.exists()) {
            String content = "set WshShell = CreateObject(\"WScript.Shell\")\n" +
                    "WshShell.AppActivate Wscript.Arguments(0)";
            writeStringToFile(file, content, StandardCharsets.UTF_8);
        }
        Runtime.getRuntime().exec("cscript " + pathname + " " + pid);
    }

    private List<String> showProcessOut(Process p) throws IOException {
        List<String> list = new ArrayList<>();
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream(), "cp866"));
        String line;
        while ((line = in.readLine()) != null) {
            list.add(line);
        }
        return list;
    }
}

主班

public class EnumerateWindows {
    public static void main(String[] args) throws IOException, InterruptedException {
        WindowSwither swither = new WindowSwither("notepad");
        swither.next();
        Thread.sleep(1000);

        swither.next();
        Thread.sleep(1000);

        swither.next();
        Thread.sleep(1000);

        swither.next();
        Thread.sleep(1000);
    }

}
于 2019-10-20T08:13:45.380 回答