edge浏览器 开启java,启动Edge浏览器,然后等待其关闭[重复]

小编典典

这是一个示例程序,可以通过某种方式设法证明硒库满足您所需的功能。您需要先下载硒库并将其设置为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/。

2020-09-28

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值