我想启动Firefox Web浏览器作为访问特定网站的过程,然后等待它关闭。
一种特殊情况是浏览器可能已经打开并正在运行,因为用户可能已经访问过某些网站。
在这种情况下,浏览器可能会在现有窗口中打开一个新选项卡,并且新启动的过程将立即终止。这不应该使我的等待过程感到困惑:要么,我想要一个新的浏览器窗口(如果可以通过命令行参数强制执行),然后等待直到将其关闭,或者保留现有的浏览器窗口,并等待所有选项卡出现从我的过程被关闭。
我认为这无关紧要,但是我的编程环境很重要Java,您可以假设我知道浏览器的路径。
Java
我可以获得其预期行为的唯一浏览器是Internet Explorer(叹气)。在这里,我基本上需要在temp文件夹中创建一个类似以下内容的新批处理脚本
start /WAIT "" "C:\Program Files\Internet Explorer\iexplore.exe" -noframemerging http://www.test.com/
然后,我运行批处理脚本,而不是直接运行浏览器,并在等待完成后将其删除。
为了使预期过程更加清晰:
noframemerging
用例是我有一个可以在本地或服务器上运行的Web应用程序。如果在本地运行,它将启动Web服务器,然后打开浏览器以访问条目页面。关闭浏览器后,该Web应用程序也应关闭。这对于Internet Explorer来说是可靠的,在所有其他情况下,用户必须关闭浏览器,然后关闭Web应用程序。因此,如果我可以可靠地等待Firefox完成,这将使用户体验更好。
按以下顺序优先选择解决方案
优先于平台无关的答案(与Windows和Linux兼容)。
原因:在理想情况下,我想知道到底做了什么,并将其包含在我自己的代码中。因为我想支持不同的浏览器(请参见下面的“ PS”),所以我希望避免每个浏览器都包含一个库。最后,我不能使用商业或封闭源代码库,但是如果没有更好的答案,我当然会接受任何可行的解决方案。我将接受类型“ 1”的第一个(相当不错的)有效答案。如果答案偏低,我将等几天再接受其中的最佳答案。
我将针对其他浏览器提出几个类似的问题。由于我认为浏览器在其摘要的命令行参数,启动线程和子流程的方式上有很大不同,因此我认为这很有意义。
这是一个示例程序,可以通过某种方式设法证明硒库满足您所需的功能。您需要先下载硒库并将其设置为IDE,然后才能运行此程序。
该程序允许您单击一个按钮。然后,Firefox浏览器会自动打开并在几秒钟内启动一个网站。网站正在加载中,请稍候。之后,您可以关闭Firefox浏览器。该程序还将在2秒后自动关闭。
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内容。有关其他信息,请参见http://www.seleniumhq.org/。