gpt4 book ai didi

java - 我如何使 if 语句中的 get 方法没有错误

转载 作者:搜寻专家 更新时间:2023-11-01 00:54:07 24 4
gpt4 key购买 nike

我想在继承自此类的其他类中使用 ids 值

if (title.equals("***")) {
String ids = driver.findElement(By.name("Idsession")).getAttribute("value");
}

我试图创建一个 get 方法,所以代码将是

 if (title.equals("***")) {
String ids = driver.findElement(By.name("Idsession")).getAttribute("value");
public String getID() {
return ids;
}
}

但是我遇到了错误

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: Syntax error on token(s), misplaced construct(s) Syntax error, insert ";" to complete Statement

at Login.initialize(Login.java:275) at Login.(Login.java:108) at Login$1.run(Login.java:91) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

完整代码

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
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.net.URL;
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.Dimension;
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.omg.CORBA.portable.InputStream;
//import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import javax.swing.JPanel;
import javax.swing.JPasswordField;


//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 daten;
private JLabel background;
private JLabel cinlabel;
private JLabel title_1;
private JLabel datalabel;
private JTextField code;
private JLabel codelabel;
private JPasswordField CIN;
private JLabel lblVersionBeta;
public WebDriver driver;

/**
* Launch the application.
*/
public static void main(String[] args) {

try {
// body of main method goes here, including any other error handling
} catch (Throwable t) {
JOptionPane.showMessageDialog(
null, t.getClass().getSimpleName() + ": " + t.getMessage());
throw t; // don't suppress Throwable
}

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();
//java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//int w=screenSize.width;
//int h=screenSize.height;
//if (w==1366 && h==768) {
frame.setBounds(100, 100, 1015, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//}else {
// frame.setBounds(100, 100, 1024, 768);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.getContentPane().setLayout(null);
//}


/*java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,screenSize.width, screenSize.height);
setVisible(true);*/

codelabel = new JLabel("Code de s\u00E9curit\u00E9 \t");
codelabel.setFont(new Font("Dubai Medium", Font.PLAIN, 18));
codelabel.setBounds(764, 279, 182, 14);
frame.getContentPane().add(codelabel);

cinlabel = new JLabel("CIN ou Identifiant DGCI ");
cinlabel.setFont(new Font("Dubai Medium", Font.PLAIN, 18));
cinlabel.setBounds(574, 74, 294, 14);
frame.getContentPane().add(cinlabel);

datalabel = new JLabel("Date de naissance (jj/mm/aaaa)\t");
datalabel.setFont(new Font("Dubai Medium", Font.PLAIN, 18));
datalabel.setBounds(574, 183, 294, 14);
frame.getContentPane().add(datalabel);
URL title =getClass().getResource("/images/title.png");

CIN = new JPasswordField();
CIN.setBounds(574, 99, 396, 50);
frame.getContentPane().add(CIN);

daten = new JTextField();
daten.setBackground(new Color(248, 248, 255));
daten.setBounds(574, 208, 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);
}

*/
//URL browser =getClass().getResource("/browser/phantomjs.exe");
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 captcha = new JLabel("");
captcha.setIcon(new ImageIcon("temp\\captcha.png"));
captcha.setBounds(574, 304, 182, 50);
frame.getContentPane().add(captcha);




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 myPass=String.valueOf(CIN.getPassword());
String date = daten.getText();
String capp = code.getText();

// fill the fields
c.sendKeys(myPass);
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")) {
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]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[1]/td[1]/div[1]/div[2]/div[1]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/p[1]"));
String name=msg.getAttribute("innerText");
//System.out.println(error);
String ids = driver.findElement(By.name("Idsession")).getAttribute("value");
private String getID()
{
return ids;
}
// String cookie=ids.getAttribute("innerText");
//JOptionPane.showMessageDialog(null,ids,"Bienvenue", JOptionPane.INFORMATION_MESSAGE);

JOptionPane.showMessageDialog(null,name,"Bienvenue", JOptionPane.INFORMATION_MESSAGE);

//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();
captcha.setIcon(img);


}


}

});



valider.setFont(new Font("Tahoma", Font.BOLD, 13));
valider.setBounds(686, 462, 162, 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, 304, 206, 50);
frame.getContentPane().add(code);

title_1 = new JLabel("");
title_1.setIcon(new ImageIcon(title));
title_1.setBounds(45, 138, 440, 90);
frame.getContentPane().add(title_1);

lblVersionBeta = new JLabel("Version Beta");
lblVersionBeta.setFont(new Font("Tahoma", Font.BOLD, 11));
lblVersionBeta.setForeground(Color.WHITE);
lblVersionBeta.setBounds(49, 201, 104, 14);
frame.getContentPane().add(lblVersionBeta);

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("");
//BufferedImage bg = ImageIO.read(new File("./resources/img/AnimationV.gif"));

URL bg =getClass().getResource("/images/AnimationV.gif");

// getClass().getResource("resources/img/AnimationV.gif");
//Image image = Toolkit.getDefaultToolkit().getImage(url);
//java.io.InputStream image = ClassLoader.getSystemResourceAsStream( "resources/img/AnimationV.gif" );

background.setIcon(new ImageIcon(bg));
background.setBounds(0, 0, 1006, 724);
frame.getContentPane().add(background);



}
}

最佳答案

不能在方法内部创建方法。您必须声明 getIds() block 像这样……

           public class name {

private String ids = "";

public static void main(String arg[]){

if (title.equals("***")) {
// reinitialize id when title is equals '***'
ids =driver.findElement(
By.name("Idsession")) .getAttribute("value");
}

String newThing = getID();
//use it
}

// method declaration here

public String getID() {
return ids;
}

}

关于java - 我如何使 if 语句中的 get 方法没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54395219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com