我第一次使用EventListener但面临异常。请任何人都可以帮助。
线程中的异常" main"显示java.lang.NullPointerException 在seleniumTest.EventListener.beforeFindBy(EventListener.java:62)
package seleniumTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebElement element ;
WebDriver driver = new FirefoxDriver();
EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
EventListener listener = new EventListener();
eventDriver.register(listener);
eventDriver.manage().window().maximize();
eventDriver.get("https://www.google.co.in");
// element = eventDriver.findElement(By.id("sb_ifc0"));
// element.sendKeys("Shantaveer");
element = eventDriver.findElement(By.xpath("//input[@type= 'submit']"));
element.click();
}
}
package seleniumTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
public class EventListener implements WebDriverEventListener{
public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("inside method afterChangeValueOf on " + arg0.toString());
}
public void afterClickOn(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("inside method afterClickOn on " + arg0.toString());
}
public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
System.out.println("Find happened on " + arg1.toString()
+ " Using method " + arg0.toString());
}
public void afterNavigateBack(WebDriver arg0) {
// TODO Auto-generated method stub
System.out.println("Inside the after navigateback to " + arg0.getCurrentUrl());
}
public void afterNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub
System.out.println("Inside the afterNavigateForward to " + arg0.getCurrentUrl());
}
public void afterNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("Inside the afterNavigateTo to " + arg0);
}
public void afterScript(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("Inside the afterScript to, Script is " + arg0);
}
public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("Inside the beforeChangeValueOf method");
}
public void beforeClickOn(WebElement arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("About to click on the " + arg0.toString());
}
public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
// TODO Auto-generated method stub
System.out.println("Just before finding element " + arg1.toString());
}
public void beforeNavigateBack(WebDriver arg0) {
// TODO Auto-generated method stub
System.out.println("Just before beforeNavigateBack " + arg0.getCurrentUrl());
}
public void beforeNavigateForward(WebDriver arg0) {
// TODO Auto-generated method stub
System.out.println("Just before beforeNavigateForward " + arg0.getCurrentUrl());
}
public void beforeNavigateTo(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("Just before beforeNavigateTo " + arg0);
}
public void beforeScript(String arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("Just before beforeScript " + arg0);
}
public void onException(Throwable arg0, WebDriver arg1) {
// TODO Auto-generated method stub
System.out.println("Exception occured at " + arg0.getMessage());
}
@Override
public void afterNavigateRefresh(WebDriver arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeNavigateRefresh(WebDriver arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
检查类似的issue 1589:
在
beforeFindBy
界面上实施afterFindBy
和WebDriverEventListener
方法时,我们会获得NullPointerException
。
webElement
未传递给侦听器。按对象正确传递。
documentation确实提到了:
void beforeFindBy(By by,
WebElement element,
WebDriver driver)
在
WebDriver.findElement(...)
,WebDriver.findElements(...)
,WebElement.findElement(...)
或WebElement.findElements(...)
之前调用。参数:
如果调用element
的查找方法,
WebDriver
- 将为null。
当我们使用除
WebElement
参数的意图是显示用作findBy
的引用的元素,而不是找到的元素。WebElement
之外的findElement
时,
WebDriver
不为空,例如使用findElement
的{{1}}方法。
所以你应该保护Webelement arg1:它可以是null,如记录所示。