【问题标题】:Why do I get java.lang.IllegalArgumentException: Cannot find elements when the XPath is null error message为什么我会收到 java.lang.IllegalArgumentException:当 XPath 为空时找不到元素错误消息
【发布时间】:2018-12-14 10:56:19
【问题描述】:

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

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

编写代码来加载此属性文件并输入用户名和密码,然后单击登录按钮。代码成功打开浏览器,但在输入用户名时,它给出“线程“主”java.lang.IllegalArgumentException:当 XPath 为空时找不到元素。”

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

【问题讨论】:

  • 文件加载成功了吗?你捕获了一个忽略 IOException ——永远不要那样做——如果你要捕获一个异常而不重新抛出一个异常,至少记录一条错误消息。
  • 我猜它无法从使 XPath 为空的属性文件中选择属性“objuserName”。你能用调试器检查一下吗?
  • 您应该花一些时间学习在 IDE 中使用调试器。尽早设置断点并单步执行代码并查看每一步的变量状态。然后你会看到错误在哪里。
  • 我将代码更改为更简单的代码: static Properties objprop = new Properties();公共静态 FileInputStream 文件输入 = null; static void propManager() 抛出 IOException { fileInput = new FileInputStream("C:\\ejagruti-Automation\\mm_finsys\\ejagruti\\src\\test\\resources\\xpath.properties"); objprop.load(fileInput);它奏效了。尽管在另一个模块中显示了相同的错误

标签: java selenium error-handling


【解决方案1】:
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.

【讨论】:

  • 我更改了 PropertyManager 代码。静态属性 objprop = new Properties();公共静态 FileInputStream 文件输入 = null; static void propManager() 抛出 IOException { fileInput = new FileInputStream("C:\\ejagruti-Automation\\mm_finsys\\ejagruti\\src\\test\\resources\\xpath.properties"); objprop.load(fileInput);它适用于上述代码,但对于其他页面,xpath 再次返回为 null.config.driver.findElement(By.name(objprop.getProperty("Company"))).click();除了使用 getProperty,我还有其他选择吗?
【解决方案2】:

请尝试以下代码,通过以下代码 sn-p 从属性文件中获取密钥:

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

}

【讨论】:

    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 2020-11-03
    相关资源
    最近更新 更多