- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在继承自此类的其他类中使用 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/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!