Webdriver selenium,java.lang.NullPointerException,同时试图找到一个Webelement [英] Webdriver selenium,java.lang.NullPointerException,while trying to locate a Webelement

查看:267
本文介绍了Webdriver selenium,java.lang.NullPointerException,同时试图找到一个Webelement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完整的代码是为了从excel获取数据并登录到Gmail,但是在尝试这样做时,我的浏览器已经打开,并且所需的页面被打开,并且登录ID是从excel中挑选并存储在变量sUsername ,但是无法将xpath定位为 element = driver.findElement(by.id(Email)); ,但是当我打印元素时,它保存为null,其中正如预期的那样,定位器ID的地址。进一步通过使用id的地址,我将使用sendkeys在文本框中输入电子邮件地址。

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.

但显示以下错误:


java.lang.NullPointerException
在appModules.SignIN.Execute(SignIN.java:21)

java.lang.NullPointerException at appModules.SignIN.Execute(SignIN.java:21)

登录类 - 定位器问题存在的位置:at - Login1.userName(driver).sendKeys(sUsername);

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;
}
}

这是我得到java null的SigniN类指针异常 - 存在问题:at- Login1.userName(driver).sendKeys(sUsername);

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;
    }
}


推荐答案

是内部处理内部等待定位元素的良好做法。如果有与页面相关的活动,那么还需要使用等待页面加载。

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);
}

显式等待

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;
}

这篇关于Webdriver selenium,java.lang.NullPointerException,同时试图找到一个Webelement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆