Java 启动Firefox并等待它关闭 问题:
我想启动Firefox web浏览器作为访问特定网站的过程,然后等待它关闭 一种特殊情况是,浏览器可能已经打开并正在运行,因为用户可能已经访问了某些网站 在这种情况下,浏览器可能会在现有窗口中打开一个新选项卡,新启动的进程将立即终止。这不应该混淆我的等待过程:或者,我想要一个新的浏览器窗口(如果可以通过某种方式强制执行,可能通过命令行参数)并等待它关闭,或者保留现有的浏览器窗口并等待我的过程产生的所有选项卡关闭 环境 我认为这无关紧要,但我的编程环境是Java 启动Firefox并等待它关闭 问题:,java,firefox,browser,process,wait,Java,Firefox,Browser,Process,Wait,我想启动Firefox web浏览器作为访问特定网站的过程,然后等待它关闭 一种特殊情况是,浏览器可能已经打开并正在运行,因为用户可能已经访问了某些网站 在这种情况下,浏览器可能会在现有窗口中打开一个新选项卡,新启动的进程将立即终止。这不应该混淆我的等待过程:或者,我想要一个新的浏览器窗口(如果可以通过某种方式强制执行,可能通过命令行参数)并等待它关闭,或者保留现有的浏览器窗口并等待我的过程产生的所有选项卡关闭 环境 我认为这无关紧要,但我的编程环境是Java,您可以假设我知道浏览器的路径 例子
Java
,您可以假设我知道浏览器的路径
例子
我可以获得预期行为的唯一浏览器是(叹气)。在这里,我需要在临时文件夹中创建一个新的批处理脚本
start /WAIT "" "C:\Program Files\Internet Explorer\iexplore.exe" -noframemerging http://www.test.com/
然后,我运行批处理脚本,而不是直接运行浏览器,并在完成等待后将其删除
预期过程
为了使预期过程更清晰:
noframemerging
,可以禁止此问题- 关于铬的类似问题:
- 关于歌剧的类似问题:
- 关于铬的类似问题:
- 关于边缘的类似问题:
- 关于狩猎的类似问题:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.net.ConnectException;
import javax.swing.*;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AnotherTest extends JFrame {
WebDriver driver;
JLabel label;
public AnotherTest() {
super("Test");
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width - 400) / 2, (screenSize.height - 100) / 2, 400, 100);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
quitApplication();
}
});
JButton jButton1 = new javax.swing.JButton();
label = new JLabel("");
JPanel panel = new JPanel(new FlowLayout());
panel.add(jButton1);
add(panel, BorderLayout.CENTER);
add(label, BorderLayout.SOUTH);
jButton1.setText("Open Microsoft");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
label.setText("Loading browser. Please wait..");
java.util.Timer t = new java.util.Timer();
t.schedule(new java.util.TimerTask() {
@Override
public void run() {
openBrowserAndWait();
}
}, 10);
}
});
}
private void openBrowserAndWait() {
driver = new FirefoxDriver();
String baseUrl = "https://www.microsoft.com";
driver.get(baseUrl);
java.util.Timer monitorTimer = new java.util.Timer();
monitorTimer.schedule(new java.util.TimerTask() {
@Override
public void run() {
while (true) {
checkDriver();
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
}
}
}
}, 10);
}
private void checkDriver() {
if (driver == null) {
return;
}
boolean shouldExit = false;
try {
label.setText(driver.getTitle());
} catch (NoSuchWindowException e) {
System.out.println("Browser has been closed. Exiting Program");
shouldExit = true;
} catch (Exception e) {
System.out.println("Browser has been closed. Exiting Program");
shouldExit = true;
}
if (shouldExit) {
this.quitApplication();
}
}
private void quitApplication() {
// attempt to close gracefully
if (driver != null) {
try {
driver.quit();
} catch (Exception e) {
}
}
System.exit(0);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AnotherTest().setVisible(true);
}
});
}
}
Selenium主要用于测试web应用程序的自动化。它可以直接打开浏览器并读取其中的html内容。有关更多信息,请参阅。我不明白“等待它关闭”是什么意思。谁在等什么?火狐?谁要关门?火狐?firefox在等待什么?等待关闭?不,我的程序正在等待。我写一个程序。该程序启动firefox并提供特定的URL进行访问。这里的“启动”意味着它将firefox作为一个新的、独立的进程启动。然后应该等到新的过程完成