为什么我得到java.lang.IollegalArgumentException:当XPath为null时找不到元素错



我创建了一个xpath.properties文件,并仅将其存储在该位置。格式为:

objuserName=//input[@placeholder='Username']
objpassword=//input[@placeholder='Password']
objloginButton=//a[@onclick='return ValidateLogin()']

编写了一个代码来加载此属性文件,并输入用户名和密码,然后单击登录按钮。代码成功地打开了浏览器,但在输入用户名时,它会给出"线程中的异常"main"java.lang.IollegalArgumentException:当XPath为null时找不到元素。">

public class Login {
static Properties objprop = new Properties();
static void PropertyManager() throws IOException{
File file = new File("C:\proj-Automation\financialsys\abcd\src\test\resources\xpath.properties");
FileInputStream fileInput = null;
try{
fileInput = new FileInputStream(file);
}catch (FileNotFoundException e){
}
Properties objprop = new Properties();
try{
objprop.load(fileInput);
}catch(IOException e){
}
//objprop.load(objfile);
}
//When user opens the "firefox" browser
void OpenBrowser(String browsername) throws IOException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",config.getParameterValue("chrome_driver_exe_path_32bit"));
config.driver=new ChromeDriver();
}
public void EnterUserName(String username){
config.driver.findElement(By.xpath(objprop.getProperty("objuserName"))).sendKeys(username);
}
public void PageMaximise(){
config.driver.manage().window().maximize();
}
//code for entering the password and clicking on login button
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
Login LF = new Login();
Login.PropertyManager();
LF.OpenBrowser("CH32");
LF.EnterURL("http://localhost:90/financialsys");
LF.PageMaximise();
LF.EnterUserName("dummycfo");
LF.EnterPassword("passw0rd");
LF.ClickLoginButton();
}
}

config.driver.findElement(By.xpath(objprop.getProperty("objuserName"((行出现IllegalArgumentException错误的原因是什么;sendKeys(username(;和LF。输入用户名("dummycfo"(;

static void PropertyManager() throws IOException{
File file = new File("C:\proj-Automation\financialsys\abcd\src\test\resources\xpath.properties");
FileInputStream fileInput = null;
try{
fileInput = new FileInputStream(file);
}catch (FileNotFoundException e){
}
try{
objprop.load(fileInput);
}catch(IOException e){
}
//objprop.load(objfile);
}
remove Properties objprop = new Properties(); this line from the above method, you are initializing objprop variable with a new object, instead of using the global one which you already have on the top.

请尝试以下代码段从属性文件中提取密钥:

public static String fetchLocatorValue(String key) throws IOException {
FileInputStream file = new FileInputStream(Path of perperty file);
Properties property = new Properties();
property.load(file);
return property.getProperty(key).toString();
}

最新更新