我正在Selenium Webdriver中运行下面显示的代码。我正在创建类SalesForceApplicationMethod()
的m1
对象,然后访问.validation()
方法。该代码的方法(.validation()
)里面去,但它给了以下错误:如何解决在java中使用硒webdriver的空指针异常
错误回溯:
Exception in thread "main" java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:212) at org.openqa.selenium.support.ui.FluentWait.(FluentWait.java:102) at org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:71) at org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:45) at com.syntel.pratice.SalesForceApplicationMethod.validation(SalesForceApplicationMethod.java:27) at com.syntel.pratice.SalesForceApplicationMethod.main(SalesForceApplicationMethod.java:88)
我的代码:
package com.syntel.pratice;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SalesForceApplicationMethod
{
WebDriver driver;
public void validation()
{
// App launcher clicking
WebDriverWait wait = new WebDriverWait(driver,25);
WebElement ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='label slds-truncate slds-text-link']")));
ele.click();
// New button
WebElement newBtn1 = driver.findElement(By.xpath("//div[contains(text(),'New')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", newBtn1);
// Account name - first account
driver.findElement(By.xpath("//input[@class='input uiInput uiInputText
uiInput--default uiInput--input']")).sendKeys("srieedherks");
// Click Save
driver.findElement(By.xpath("//button[@class='slds-button slds-button--neutral uiButton--default uiButton--brand uiButton forceActionButton']")).click();
// Clicking new contact button
WebDriverWait wait1 = new WebDriverWait(driver, 20);
WebElement newbt = driver.findElement(By.xpath("//div[contains(text(),'New
Contact')]"));
JavascriptExecutor exe = (JavascriptExecutor)driver;
exe.executeScript("arguments[0].click();", newbt);
WebElement ele1 = driver.findElement(By.linkText("--None--"));
ele1.sendKeys("Mr.");
System.out.println("Selecting ");
// Entering first name
driver.findElement(By.xpath("//input[@class='compoundBorderBottom form-
element__row input']")).sendKeys("srieedher");
// Entering last name
driver.findElement(By.xpath("//input[@class='compoundBLRadius
compoundBRRadius form-element__row input']")).sendKeys("santhakumar");
// Click Save
driver.findElement(By.xpath("//button[@class='slds-button slds-button--
brand cuf-publisherShareButton undefined uiButton']")).click();
// Logout view profile
driver.findElement(By.xpath("//img[@src='https://c.ap5.content.force.com/pro
filephoto/005/T/1']")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", newbt);
// Click log out
driver.findElement(By.xpath("//a[contains(@class, 'profile-link-label
logout uiOutputURL')]")).click();
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
ChromeOptions o = new ChromeOptions();
o.addArguments("disable-extensions");
o.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(o);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://login.salesforce.com/");
driver.findElement(By.id("username")).sendKeys("[email protected]");
driver.findElement(By.id("password")).sendKeys("Sriee678$");
driver.findElement(By.id("Login")).click();
Scanner s = new Scanner(System.in);
System.out.println("Enter your otp:");
String i = s.next().toString();
System.out.println("the OTP is : "+i);
driver.findElement(By.id("smc")).sendKeys(i);
driver.findElement(By.id("save")).click();
driver.findElement(By.xpath("//div[@class='slds-icon-Waffle']")).click();
SalesForceApplicationMethod m1 = new SalesForceApplicationMethod();
m1.validation();
}
}
你能添加完整的错误信息吗? – Asew
就像@Asew指出的那样,没有完整的错误追溯,很难调试你的问题。 '代码进入方法内部(哪个方法?)',但是它会给出错误,因为方法第一行中的空指针异常(这是第一行?)并没有给我们提供任何信息。 :) – iamdanchiv
@SrieedherSanthakumar您没有为'm1'实例设置''驱动程序' –