簡體   English   中英

為什么我得到java.lang.IllegalArgumentException:XPath為null時找不到元素錯誤消息

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

我創建了一個xpath.properties文件,並將其僅存儲在該位置。 格式為:

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

編寫代碼以加載此屬性文件,然后輸入用戶名和密碼,然后單擊“登錄”按鈕。 該代碼成功打開了瀏覽器,但是在輸入用戶名時,它給出了“線程“ 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();
}
}

config.driver.findElement(By.xpath(objprop.getProperty(“ objuserName”)))。sendKeys(username)處的IllegalArgumentException錯誤可能是什么原因? 和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.

請嘗試以下代碼,通過以下代碼片段從屬性文件中獲取密鑰:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM