excel - Webdriver selenium,java.lang.NullPointerException,while trying to locate a Webelement -
complete code written fetch data excel , login gmail, while trying browser had opened , desired page got opened , login id picked excel , stored in variable susername, unable locate xpath as- element=driver.findelement(by.id("email"));
when print element holds "null", expected address of locator id. further using address of id had used sendkeys enter email address in text box.
but following error displayed:
java.lang.nullpointerexception @ appmodules.signin.execute(signin.java:21)
login class-where locator issue exists: @ - 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 signin class iam getting 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 have instantiate 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 driver 10 seconds"); driver.get(constant1.url); // log.info("web application launched successfully"); } return driver; } }
it practice deal internally explicit wait locating element. if there page related activity need use wait page load.
please follow bellow code internal wait
protected webelement waitforpresent(final string locator) { // timeout 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; }
Comments
Post a Comment