Here is a method I have written:
public void delayToCapture(String methodGetBy, String key){
/* List of methodGetBy:
* 1. getElementById
* 2. getElementsByTagName
* 3. getElementsByClassName
* 4. querySelectorAll
*/
System.out.println("Excuteing javaScript...");
if(methodGetBy.equals("getElementById")){
js.executeScript("setTimeout(function(){ document." +methodGetBy+ "('" +key+ "').setAttribute('style', 'display: none');},500);");
}
else if(methodGetBy.equals("getElementsByClassName")){
js.executeScript("setTimeout(function(){"
+ "var elems = document.getElementsByClassName('"+ key +"');"
+ "for(var i = 0; i < elems.length; i++){"
+ "elems[i].style.display = 'none';}"
+ "},"
+ "500);");
}
}
Now, let me show you how I call that method in another class:
delayToCapture("getElementsByClassName", "positionmenu");
Every time I run the code, the console displays this error message:
java.lang.NullPointerException
However, the interesting part is that when I directly run the following code in the browser console, it works perfectly fine:
setTimeout(function(){
var elems = document.getElementsByClassName('positionmenu');
for(var i = 0; i < elems.length; i++){
elems[i].style.display = 'none';
}
},500);
So, can someone please enlighten me on what might be causing this issue?