如何解决控制台在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 举报,一经查实,本站将立刻删除。