【发布时间】:2019-04-21 15:33:04
【问题描述】:
我曾尝试创建方法并将其从另一个文件调用到主类,但它不起作用,错误消息说“java.lang.NullPointerException”
主类
Keywords kw = new Keywords();
@When("^gmailDD$")
public void gmailDD() throws Throwable{
WebDriverWait wait5s = new WebDriverWait(driver, 5);
String regis = "/html/body/div[2]/div[1]/div[5]/ul[1]/li[3]/a";
String dd = "/html/body/div[1]/div/footer/div/div/div[1]";
String empty = "/html/body/div[1]/div/footer";
kw.clickbyxpath(regis);
String handle= driver.getWindowHandle();
System.out.println(handle);
// Store and Print the name of all the windows open
Set handles = driver.getWindowHandles();
System.out.println("Log window id: "+handles);
driver.switchTo().window("6442450949");
kw.clickbyxpath(empty);
kw.clickbyxpath(dd);
}`
方法类
WebDriver saddriver;
public void clickbyxpath (String xpathvalue) throws InterruptedException, IOException
{
WebDriverWait sad = new WebDriverWait(saddriver, 10);
//To wait for element visible
System.out.println(xpathvalue);
String x = xpathvalue;
sad.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(x)));
wowdriver.findElement(By.xpath(x)).click();
}
我曾尝试在同一个文件中进行相同的编码,它没有问题,但是当我将 Method.class 移动到新文件时,错误消息说“java.lang.NullPointerException”但我可以获得“xpathvalue”值.
【问题讨论】:
-
能否请您发布您的错误堆栈跟踪以获取更多详细信息。可能是找不到你的窗口。
-
“kw.clickbyxpath(regis);”处显示的错误消息在 Main.class 文件中,第二个错误显示在 Method.class 文件中的“WebDriverWait sad = new WebDriverWait(saddriver, 10);”中
-
我已经阅读了这些错误日志,它是关于 WebDriver 和 WebDriverWait 的。
-
发生这种情况是因为它找不到您的驱动程序实例。
标签: java selenium selenium-webdriver methods parameter-passing