selenium-java,暂停功能
暂停/开始应用程序
public class Common_method { public void kill_chromedriver(){ Runtime runtime=Runtime.getRuntime(); try{ System.out.println("kill chromedriver.exe"); runtime.exec("taskkill /f /im chromedriver.exe"); }catch(Exception e){ System.out.println("Error!"); } } public void kill_geckodriver(){ Runtime runtime=Runtime.getRuntime(); try{ System.out.println("kill geckodriver.exe"); runtime.exec("taskkill /f /im geckodriver.exe"); }catch(Exception e){ System.out.println("Error!"); } } public void paused_start(FindElement findElement_Re){ JFrame frame = new JFrame("开始/暂停");//新建一个窗体,窗体的标题是""中的内容, frame.setSize(200,100);//设置窗体的尺寸、Dimension frameSize=frame.getSize(); Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕尺寸对象 //frame.setLocation((screenSize.width-frame.getWidth())/2, (screenSize.height-frame.getHeight())/2);//设置窗体坐标 frame.setLocation(screenSize.width-220, screenSize.height-150); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); //定义了点击关闭按钮的作用; frame.setAlwaysOnTop(true);//把应用置于最前 frame.setVisible(true);//注意这一步一定要调用,使得这个窗体可见,false是不可见; //JPanel panel = new JPanel();//Jpanel 是面板容器类,包含在swing中; JButton jButton1 = new JButton("暂停");//定义一个button frame.add(jButton1); jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(findElement_Re.get_paused_start()==0){ findElement_Re.paused(); System.out.println("暂停--------------------------------------------------"); jButton1.setText("开始"); }else if(findElement_Re.get_paused_start()==1){ findElement_Re.start(); System.out.println("开始--------------------------------------------------"); jButton1.setText("暂停"); } } }); } }
方法
public class FindElement { public int paused_start = 0; public int get_paused_start(){ //返回是否暂停,0开始,1暂停 return paused_start; } public void paused(){ //暂停 paused_start = 1; } public void start(){ //开始 paused_start = 0; } public WebElement findelement(WebElement webElement){ while(paused_start==1){//判断是否暂停了 System.out.println(paused_start); } return webElement; } }
使用方法
FindElement findElement_Re = new FindElement();
findElement_Re.findelement(webDriver1.findElement(By.cssSelector("[class='share-tag share-to-earn']"))).click();