简体   繁体   English

为什么我得到java.lang.IllegalArgumentException:XPath为null时找不到元素错误消息

[英]Why do I get java.lang.IllegalArgumentException: Cannot find elements when the XPath is null error message

I have created a xpath.properties file and stored it at the location only. 我创建了一个xpath.properties文件,并将其仅存储在该位置。 The format is as: 格式为:

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

Written a code to load this property file and enter the username and password and click login button. 编写代码以加载此属性文件,然后输入用户名和密码,然后单击“登录”按钮。 The code opens the browser successfully but on entering the username it gives "Exception in thread "main" java.lang.IllegalArgumentException: Cannot find elements when the XPath is null." 该代码成功打开了浏览器,但是在输入用户名时,它给出了“线程“ main”中的异常java.lang.IllegalArgumentException: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();
}
}

What could be the reason of the IllegalArgumentException error at line config.driver.findElement(By.xpath(objprop.getProperty("objuserName"))).sendKeys(username); config.driver.findElement(By.xpath(objprop.getProperty(“ objuserName”)))。sendKeys(username)处的IllegalArgumentException错误可能是什么原因? and LF.EnterUserName("dummycfo"); 和LF.EnterUserName(“ 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.

Kindly try the below code to fetch the key from property file by below code snippet: 请尝试以下代码,通过以下代码片段从属性文件中获取密钥:

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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalArgumentException:“原始”消息参数不能为null - java.lang.IllegalArgumentException: The 'original' message argument cannot be null java.lang.IllegalArgumentException: uri 不能是 null - java.lang.IllegalArgumentException: uri cannot be null 为什么在Netbeans中得到错误:java.lang.IllegalArgumentException和java.lang.reflect.InvocationTargetException? - Why Do I Get The Errors: java.lang.IllegalArgumentException and java.lang.reflect.InvocationTargetException In Netbeans? 错误消息:java.lang.IllegalArgumentException - Error message: java.lang.IllegalArgumentException java.lang.IllegalArgumentException:im == null! 错误 - java.lang.IllegalArgumentException: im == null! error 为什么我收到此错误java.lang.IllegalArgumentException? - Why am I getting this error java.lang.IllegalArgumentException? 在JAVA中上载时发生错误“ message [java.lang.IllegalArgumentException:im == null!]” - Error While Uploading in JAVA “message[java.lang.IllegalArgumentException: im == null!]” 为什么会有java.lang.IllegalArgumentException? - Why is there an java.lang.IllegalArgumentException? 为什么会出现java.lang.IllegalArgumentException? - Why java.lang.IllegalArgumentException? 休眠查询java.lang.IllegalArgumentException:要遍历的节点不能为null - hibernate query java.lang.IllegalArgumentException: node to traverse cannot be null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM