java - 为什么我会收到 java.lang.IllegalArgumentException:当 XPath 为空时找不到元素错误消息
问题描述
我创建了一个 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");
解决方案
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.
推荐阅读
- python - Pandas:对于排序序列 B 的所有元素,查找排序序列 A 中最近元素的索引
- python - 列表到 Numpy 数组和重塑(维度问题)
- javascript - Phaser 3:使用对象作为 Phaser.Scene 属性
- php - 我开发的wordpress插件突然出现异常错误
- python - anaconda 3 基础环境设置错误的 PYTHONPATH
- reactjs - useSelector 不会从 Redux 存储返回当前值
- c++ - 霍夫曼编码树的最大高度
- reactjs - 来自 React 项目的长期累积警告列表。保留它们还是清除它们?
- python - 有没有办法用 PyGTK AppIndicator 检测鼠标点击
- mysql - Laravel 内连接的内连接