问题
Complete code is written to fetch data from excel and login to Gmail, but while trying to do so my browser had opened and also the desired page got opened and as well as login id was picked from excel and stored in the variable sUsername, but unable to locate the xpath as- element=driver.findElement(by.id("Email"));
but when I print element it holds "null", where as expected was some address of the locator id. Further by using the address of id I would had used with sendkeys to enter the email address in the text box.
But the following error was displayed:
java.lang.NullPointerException at appModules.SignIN.Execute(SignIN.java:21)
Login class-where the locator issue exists: at - Login1.userName(driver).sendKeys(sUsername);
public class Login1 {
//private static WebDriver driver=null;
private static WebElement element=null;
public static WebElement userName(WebDriver driver)
{
try {
System.out.println("aaa");
System.out.println("bb");
element=driver.findElement(By.name("Email"));
System.out.println("ccc");
} catch (Exception e) {
// TODO: handle exception
System.out.println(element);
}
return element;
}
public static WebElement btn_login(WebDriver driver)
{
element= driver.findElement(By.id("next"));
return element;
}
public static WebElement passWord(WebDriver driver)
{
element= driver.findElement(By.id("Passwd"));
return element;
}
public static WebElement btn_SignIN(WebDriver driver)
{
element= driver.findElement(By.id("signIn"));
return element;
}
}
This is the SigniN class where iam getting the java null pointer exception--issue exists: at- Login1.userName(driver).sendKeys(sUsername);
public class SignIN {
private static WebDriver driver=null;
public static void Execute (int iTestCaseRow)
{
String sUsername=ExcelUtils1.getCellData(iTestCaseRow,Constant1.col_UserName);
System.out.println(sUsername);
//driver.ma3nage().window().maximize();
//driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Login1.userName(driver).sendKeys(sUsername);
//driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Login1.btn_login(driver).click();
String pass=ExcelUtils1.getCellData(iTestCaseRow, Constant1.col_password1);
Login1.passWord(driver).sendKeys(pass);
Login1.btn_SignIN(driver).click();
}
}
This is where I have instantiate the browser--
public class Utils1 {
public static WebDriver driver;
public static WebDriver OpenBrowser(int iTestCaseRow) {
String sBrowserName;
System.out.println(iTestCaseRow);
sBrowserName = ExcelUtils1.getCellData(iTestCaseRow,
Constant1.col_browser);
if (sBrowserName.equals("Mozilla")) {
driver = new FirefoxDriver();
// Log.info("New driver instantiated");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Log.info("Implicit wait applied on the driver for 10 seconds");
driver.get(Constant1.URL);
// Log.info("Web application launched successfully");
}
return driver;
}
}
回答1:
It is good practice to deal with internally as well explicit wait for locating element. If there is page related activity then also need to use wait for page to load.
Please follow bellow code For internal Wait
protected WebElement waitForPresent(final String locator) {
// timeout is your default wait timeout in long.
return waitForPresent(locator, timeout);
}
For Explicit Wait
protected WebElement waitForPresent(final String locator, long timeout) {
WebDriverWait wait = new WebDriverWait(driver, timeout);
WebElement ele = null;
try {
ele = wait.until(ExpectedConditions
.presenceOfElementLocated(locator));
} catch (Exception e) {
throw e;
}
return ele;
}
protected WebElement waitForNotPresent(final String locator, long timeout) {
timeout = timeout * 1000;
long startTime = System.currentTimeMillis();
WebElement ele = null;
while ((System.currentTimeMillis() - startTime) < timeout) {
try {
ele = findElement(locator);
Thread.sleep(1000);
} catch (Exception e) {
break;
}
}
return ele;
}
回答2:
Just spit balling here, but in addition to the copy/paste issues stated above.. I don't see where you do a 'get' to load the gmail page for the driver instance you are creating? Something like..
driver.get("https://mail.google.com/something");
Also, it would probably be a good idea to put an explicit wait in place for the "Email" field before doing the findElement as the page may still be rendering:
Wait<WebDriver> doFluentWait = fluentWait = new FluentWait<>(driver).withTimeout(PAGE_LOAD_WAIT, TimeUnit.SECONDS)
.pollingEvery(POLLING_INTERVAL, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
and then do something like
doFluentWait.until(WebDriverUtil.elementIsVisible(By.name("Email")));
来源:https://stackoverflow.com/questions/35041341/webdriver-selenium-java-lang-nullpointerexception-while-trying-to-locate-a-webel