控制台在Java中终止时如何生成一些事件?

如何解决控制台在Java中终止时如何生成一些事件?

我正在使用java.enter代码中的硒库进行爬网

是登录->移动页面->输入的搜索关键字->爬行数据表

我将源文件设为jar-> exe。

我的源代码以无头模式执行chromedriver。

无论如何,我在Eclipse中执行了exe或.java文件。 当我单击exe中的“ X”按钮或单击控制台(Eclipse)中的红色按钮(终止)时

背景chromedriver和Google chrome无法关闭。

如何关闭后台chromedriver?

我认为我的动作就像关闭主要方法,而不是异常关闭 (因为当我输入ctrl + z时:它正确运行,因此背景chromedriver已关闭。)

这是我的消息来源...我需要您的帮助:

package com.sqisoft.thingplug;
import java.util.List;
import java.util.Scanner;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.sqisoft.thingplug.DataParsing;
public class Selenium_ThingPlug {
private static WebElement btn_login_first;
private static WebElement text_box_id;
private static WebElement text_box_pw;
private static WebElement btn_login_second;
private static WebElement btn_menu;
private static WebElement btn_device_log_monitoring;
private static WebElement radio_device_id;
private static WebElement keyword_search;
private static WebElement btn_search;
private static WebElement device_id_list;
private static WebElement main_list;
private static WebElement device_manage;
private static String DRIVER_ID = "webdriver.chrome.driver";
private static String DRIVER_PATH = "chromedriver.exe";
private static String base_url = "https://thingplugsvc.sktiot.com:10082";
private static WebDriver driver;
private static ChromeOptions options;
private static Scanner scan;
//System.setProperty(DRIVER_ID,DRIVER_PATH);



static void modifyData(String data) {
    String result=data.trim();
    ParsingData(result);
    
}


static void ParsingData(String data) {
    
    String key=data.substring(2,4);
    String equip_type=data.substring(4,6);
    String multiStr=data.substring(6,8);
    String errorCheckData=data.substring(8,10);
    String dataValue=data.substring(10);
    
    switch (key) {
    case "01":
        System.out.print("solarlight/");
        
            if(equip_type.equals("01")) {
                System.out.print("singlephase");
            }
            else if(equip_type.equals("02")) {
                System.out.print("threephase");
            }
            
            checkMulti(multiStr);
            
            if(isErrorData(errorCheckData)==true) {
                if(data.length()==62)
                    DataParsing.parseSolarLight_Single(dataValue);
                else if(data.length()==86)
                    DataParsing.parseSolarLight_Three(dataValue);
                else
                    System.out.print("length is not correct.");
            }
        
        break;
    case "02":
        System.out.print("solarheat/");
        if(equip_type.equals("01"))
            System.out.print("solarheat_force");
        else if(equip_type.equals("02"))
            System.out.print("solarheat_natural");
        
        checkMulti(multiStr);
        if(isErrorData(errorCheckData)==true) {
            if(data.length()==86)
                DataParsing.parseSolarHeat_Forced(dataValue);
            else if(data.length()==46)
                DataParsing.parseSolarHeat_Nature(dataValue);
            else
                System.out.print("length is not correct.");
        }
        break;
    case "03":
        System.out.print("heatpump/");
        if(equip_type.equals("01"))
            System.out.print("heatpump");
        else if(equip_type.equals("02"))
            System.out.print("heatpump_buha");
        
        checkMulti(multiStr);
        if(isErrorData(errorCheckData)==true) {
            if(data.length()==92)
                DataParsing.parseGeoHeat_HeatPump(dataValue);
            else if(data.length()==78)
                DataParsing.parseGeoHeat_Buha(dataValue);
            else
                System.out.print("length is not correct");
        }
        break;
    case "04":
        System.out.print("windpower/");
        if(equip_type.equals("01"))
            System.out.print("static type");
        else
            System.out.print("wrong type");
        
        checkMulti(multiStr);
        if(isErrorData(errorCheckData)==true) {
            if(data.length()==58)
                DataParsing.parseWindPower(dataValue);
            else
                System.out.print("length is not correct.");
        }
        break;
    case "06":
        System.out.print("fuel cell/");
        if(equip_type.equals("01"))
            System.out.print("static type");
        else
            System.out.print("wrong type");
        
        checkMulti(multiStr);
        if(isErrorData(errorCheckData)==true) {
            if(data.length()==122)
                DataParsing.parseFuelCell(dataValue);
            else
                System.out.print("length is not correct.");
        }
        break;
    case "07":
        System.out.print("ESS/");
        if(equip_type.equals("01"))
            System.out.print("static type");
        else
            System.out.print("wrong type");
        
        checkMulti(multiStr);
        if(isErrorData(errorCheckData)==true) {
            if(data.length()==64)
                DataParsing.parseESS(dataValue);
            else
                System.out.print("length is not correct");
        }
        break;
    default:
        break;
    }
    
}

static void checkMulti(String data) {

    if(data.equals("00"))
        System.out.print("/equip 1");
    else if(data.equals("01"))
        System.out.print("/equip 2");
    else if(data.equals("02"))
        System.out.print("/equip 3");
    else if(data.equals("03"))
        System.out.print("/equip 4");
}

static boolean isErrorData(String data) {
    if(data.equals("38")) {
        System.out.print("/serial port open fail");
        return false;
    }
    else if(data.equals("39")) {
        System.out.print("/fail connection with equip");
        return false;
    }
    else if(data.equals("00")) {
        System.out.print("/correct");
        return true;
    }
    return false;
}

static class Bye extends Thread{
    public void run() {         
        driver.quit();
        
        System.exit(0);
    }
}


static void make_inquiry(WebDriver driver) throws InterruptedException {
    
    Thread.sleep(1000);
    radio_device_id=driver.findElement(By.xpath("//input[@name='cond' and @value='1']"));
    radio_device_id.click();
    
    scan=new Scanner(System.in);
    System.out.println("device id: ");
    String device_id=scan.next();
    Runtime.getRuntime().addShutdownHook(new Bye());
    keyword_search=driver.findElement(By.className("input_text_type1"));
    keyword_search.clear();
    keyword_search.sendKeys(device_id);
    //keyword_search.sendKeys("00000417702c1ffffe5bde2e");
    
    System.out.println("searching data....");
    
    Thread.sleep(3000);
    btn_search=driver.findElement(By.cssSelector("#btnSearch"));
    btn_search.click();
    
    
}

public static void main(String[] args) throws InterruptedException,NoSuchElementException,Exception {
    
    Runtime.getRuntime().addShutdownHook(new Bye());
    
    scan=new Scanner(System.in);
    System.out.println("--------ThingPlug Data Checker-------");
    
    System.setProperty(DRIVER_ID,DRIVER_PATH);

    
    options=new ChromeOptions();
    options.addArguments("headless");
    driver = new ChromeDriver(options);
    
    System.out.println("connecting....");
    driver.get(base_url);
    Thread.sleep(1000);
    
    
    
    System.out.print("ID :");
    String thingplug_id=scan.next();
    System.out.print("\n");
    
    System.out.print("PW :");
    String thingplug_pw=scan.next();
    
    System.out.println("Thingplug  "+thingplug_id+" login");
    
    
    try {

    btn_login_first=driver.findElement(By.id("log_in"));
    btn_login_first.click();
    Thread.sleep(500);
    
    text_box_id=driver.findElement(By.id("id"));
    text_box_id.clear();
    text_box_id.sendKeys(thingplug_id);
    
    
    text_box_pw=driver.findElement(By.id("password"));
    text_box_pw.clear();
    text_box_pw.sendKeys(thingplug_pwd);

    Thread.sleep(1000);
    btn_login_second=driver.findElement(By.className("btn_blue_big"));
    btn_login_second.click();
    
    
    Thread.sleep(1000);
    btn_menu=driver.findElement(By.className("btn_gnb_area"));
    }catch(NoSuchElementException e) {
        System.out.println("no account,restart program");
        driver.quit();
        Thread.sleep(3000);
        System.exit(0);
    }
    
    btn_menu.click();
    
    Thread.sleep(1000);
    btn_device_log_monitoring=driver.findElement(By.id("M010002130"));
    btn_device_log_monitoring.click();
    
    
    Runtime.getRuntime().addShutdownHook(new Bye());
    
    
    while(true) {
    make_inquiry(driver);
    
    try {
    Thread.sleep(3000);
    device_id_list=driver.findElement(By.cssSelector("#divList > div > table > tbody > tr > td:nth-child(1) > a"));
    }catch(NoSuchElementException e) {
        System.out.println("no device id");
        Thread.sleep(1000);
        device_manage=driver.findElement(By.cssSelector("#M010002130 > a > span"));
        device_manage.click();
        make_inquiry(driver);
    }
    Thread.sleep(2000);
    device_id_list=driver.findElement(By.cssSelector("#divList > div > table > tbody > tr > td:nth-child(1) > a"));
    device_id_list.click();
    
    
    
    Thread.sleep(3000);
    List<WebElement> col=driver.findElements(By.xpath("//*[@id=\"divList\"]/div/div/table/thead/tr/th"));
    List<WebElement> row=driver.findElements(By.xpath("//*[@id=\"divList\"]/div/div/table/tbody/tr/td[1]"));
    
    System.out.println(row.size()+"data");
    System.out.println("only five is displayed");
    System.out.print("\n");
    /*
    #divList > div > div > table > tbody > tr:nth-child(1) > td:nth-child(1)
    #divList > div > div > table > tbody > tr:nth-child(1) > td:nth-child(2)
    
    #divList > div > div > table > tbody > tr:nth-child(2) > td:nth-child(1)
    */
    int k=1;
    
    if(row.size()!=0) {
    
    for(int i=1;i<row.size()+1;i++) {
        for(int j=1;j<col.size()+1;j++) {
            String data=driver.findElement(By.cssSelector("#divList > div > div > table > tbody > tr:nth-child("+i+") > td:nth-child("+j+")")).getText();
            if(k%2==1)
                System.out.print(data+" ");
            else if(k%2==0)
                System.out.print(data+"\n");
            
            k++;
            if(j%2==0) {
                modifyData(data);
                }
            }//end for
        
        System.out.print("\n");
        if(i>4) {
            break;
            }
        }//end for
    }//end if
    else {
        System.out.println("no data\n");
    }
    
    
    /////////////////////////////////////////////////////////////////
    
    Thread.sleep(1000);
    main_list=driver.findElement(By.cssSelector("#frmSearch > div.btn_area.clearfix > div > ul > li > span > a"));
    main_list.click();
    
    }//while
    
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-