我有3个班级,分别是Login / Accueil / CarteEtd
登录类具有phantomjs,可打开指向要登录的网站的链接
如果登录成功,则显示Accueil类,如果用户单击按钮,它将调用CarteEtd JFrame
CarteEtd课程是获取登录用户/学生的学校身份证
这是网站:https://www4.inscription.tn/ORegMx/servlet/AuthentificationEtud?ident=cin
我如何使用在CarteEtd类的Login类中让用户登录的窗口
我使用屏幕截图获取验证码和学生证之类的信息
由于扩展了Jframe,我无法继承,我也尝试在登录类中创建一个get方法来获取打开的窗口,但未成功
注意:链接的任何新窗口/标签打开,会话将断开连接,这就是为什么我需要在所有代码/类中使用记录的窗口的原因 Login.class
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
//import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
/*
import java.io.IOException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
*/
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import javax.swing.ImageIcon;
import javax.swing.JButton;
//import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
//import javax.swing.plaf.metal.MetalBorders.TextFieldBorder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
//import java.awt.Point;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.apache.commons.io.FileUtils;
//import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import javax.swing.JPanel;
//import java.io.File;
//import java.io.IOException;
//import java.util.concurrent.TimeUnit;
//import org.apache.commons.io.FileUtils;
//import org.openqa.selenium.OutputType;
//import org.openqa.selenium.TakesScreenshot;
//import org.openqa.selenium.chrome.ChromeDriver;
public class Login extends JFrame {
private JFrame frame;
private JTextField CIN;
private JTextField daten;
private JLabel background;
private JLabel cinlabel;
private JLabel lblNewLabel_1;
private JLabel datalabel;
private JTextField code;
private JLabel codelabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login window = new Login();
//window.frame.setUndecorated(true);
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
//PropertyConfigurator.configure("C:\\Users\\MJ\\eclipse-workspace\\FindMyResults\\src\\log4j.properties");
}
/**
* Create the application.
* @throws IOException
*/
public Login() throws IOException {
initialize();
}
/**
* Initialize the contents of the frame.
* @throws IOException
*/
private void initialize() throws IOException {
frame = new JFrame();
frame.setBounds(100, 100, 1024, 768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
codelabel = new JLabel("Code de s\u00E9curit\u00E9 \t");
codelabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
codelabel.setBounds(764, 374, 182, 14);
frame.getContentPane().add(codelabel);
cinlabel = new JLabel("CIN ou Identifiant DGCI ");
cinlabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
cinlabel.setBounds(574, 169, 294, 14);
frame.getContentPane().add(cinlabel);
datalabel = new JLabel("Date de naissance \t");
datalabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
datalabel.setBounds(574, 278, 182, 14);
frame.getContentPane().add(datalabel);
lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\MJ\\Desktop\\giphy.gif"));
lblNewLabel_1.setBounds(616, 64, 440, 90);
frame.getContentPane().add(lblNewLabel_1);
CIN = new JTextField();
CIN.setBackground(new Color(248, 248, 255));
CIN.setSelectedTextColor(new Color(165, 42, 42));
CIN.setSelectionColor(new Color(175, 238, 238));
CIN.setBounds(574, 201, 396, 50);
CIN.setToolTipText("");
CIN.setFont(new Font("Helvetica", Font.PLAIN, 18));
frame.getContentPane().add(CIN);
CIN.setColumns(10);
daten = new JTextField();
daten.setBackground(new Color(248, 248, 255));
daten.setBounds(574, 303, 396, 50);
daten.setForeground(new Color(0, 0, 0));
daten.setFont(new Font("Helvetica", Font.PLAIN, 18));
daten.setColumns(10);
frame.getContentPane().add(daten);
/*
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
try {
editorPane.setPage(url);
//editorPane.scrollToReference(reference);
} catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + url);
}
*/
System.setProperty("phantomjs.binary.path", "phantomjs.exe");
WebDriver driver = new PhantomJSDriver();
//driver.manage().Window.Size = new Size(1920, 1080);
driver.get("https://www4.inscription.tn/ORegMx/servlet/AuthentificationEtud?ident=cin");
String title1=driver.getTitle();
if (!(title1.equals("Site de l'inscription universitaire en ligne"))) {
JOptionPane.showMessageDialog(null,"Verifiez votre connexion internet");
driver.quit();
}
WebElement ele = driver.findElement(By.xpath("//td[@rowspan='2']"));
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
org.openqa.selenium.Point point = ele.getLocation();
// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("temp\\captcha.png");
FileUtils.copyFile(screenshot, screenshotLocation);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon("temp\\captcha.png"));
lblNewLabel.setBounds(574, 399, 182, 50);
frame.getContentPane().add(lblNewLabel);
JButton valider = new JButton("Valider");
valider.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
WebElement c = driver.findElement(By.name("cin"));
WebElement d = driver.findElement(By.id("dn"));
WebElement cap = driver.findElement(By.name("cincap"));
String cin = CIN.getText();
String date = daten.getText();
String capp = code.getText();
// fill the fields
c.sendKeys(cin);
d.sendKeys(date);
cap.sendKeys(capp);
// button valider
cap.submit();
// check the title of the page
String title=driver.getTitle();
if (title.equals("Dashboard - Inscription universitaire en ligne")) {
//System.out.println("Bienvenue"); //modify to new frame soon
frame.dispose();
Accueil bien=new Accueil();
bien.setVisible(true);
// driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
}
else {
WebElement msg = driver.findElement(By.xpath("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/blockquote[1]/div[1]/font[1]"));
String error=msg.getAttribute("innerText");
//System.out.println(error);
JOptionPane.showMessageDialog(null,error);
WebElement ele = driver.findElement(By.xpath("//td[@rowspan='2']"));
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = null;
try {
fullImg = ImageIO.read(screenshot);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the location of element on the page
org.openqa.selenium.Point point = ele.getLocation();
// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
try {
ImageIO.write(eleScreenshot, "png", screenshot);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Copy the element screenshot to disk
File screenshotLocation = new File("temp\\captcha.png");
try {
FileUtils.copyFile(screenshot, screenshotLocation);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageIcon img=new ImageIcon("temp\\\\captcha.png");
img.getImage().flush();
lblNewLabel.setIcon(img);
}
}
});
valider.setFont(new Font("Tahoma", Font.BOLD, 13));
valider.setBounds(722, 557, 104, 32);
frame.getContentPane().add(valider);
code = new JTextField();
code.setBackground(new Color(248, 248, 255));
code.setForeground(Color.BLACK);
code.setFont(new Font("Helvetica", Font.PLAIN, 18));
code.setColumns(10);
code.setBounds(764, 399, 206, 50);
frame.getContentPane().add(code);
JPanel panel = new JPanel();
panel.setBackground( new Color(0, 0, 0, 90) );
panel.setBounds(0, 0, 511, 735);
frame.getContentPane().add(panel);
background = new JLabel("");
background.setIcon(new ImageIcon("E:\\AnimationV.gif"));
background.setBounds(0, 0, 1006, 724);
frame.getContentPane().add(background);
}
}
Accueil.class
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.UIManager;
public class Accueil extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Accueil frame = new Accueil();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Accueil() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1024, 768);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("");
btnNewButton.setForeground(UIManager.getColor("Button.highlight"));
btnNewButton.setBackground(UIManager.getColor("CheckBox.background"));
btnNewButton.setIcon(new ImageIcon("C:\\Users\\MJ\\eclipse-projects\\FindMyResults\\img\\business-card-of-a-man-with-contact-info.png"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CarteEtd newcarte=new CarteEtd();
newcarte.setVisible(true);
}
});
btnNewButton.setBounds(47, 40, 417, 188);
contentPane.add(btnNewButton);
}
}
CarteEtd.class
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import javax.swing.JLabel;
public class CarteEtd extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CarteEtd frame = new CarteEtd();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CarteEtd() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1024, 768);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBounds(80, 76, 647, 314);
contentPane.add(lblNewLabel);
ImageIcon img=new ImageIcon("temp\\\\carteEtd.png");
img.getImage().flush();
lblNewLabel.setIcon(img);
WebDriver driver = new PhantomJSDriver();
WebElement ele = driver.findElement(By.xpath("//p[contains(text(),'Carte Etudiant')]"));
Actions builder = new Actions(driver);
builder.moveToElement(ele).click(ele);
builder.perform();
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = null;
try {
fullImg = ImageIO.read(screenshot);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the location of element on the page
org.openqa.selenium.Point point = ele.getLocation();
// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
try {
ImageIO.write(eleScreenshot, "png", screenshot);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Copy the element screenshot to disk
File screenshotLocation = new File("temp\\captcha.png");
try {
FileUtils.copyFile(screenshot, screenshotLocation);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
希望有人有办法