简体   繁体   English

尝试查找Webelement时,Webdriver selenium,java.lang.NullPointerException

[英]Webdriver selenium,java.lang.NullPointerException,while trying to locate a Webelement

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")); 编写了完整的代码以从excel中获取数据并登录到Gmail,但是在尝试这样做时,我的浏览器已经打开,并且还打开了所需的页面,并且从excel中选择了登录ID,并将其存储在变量sUsername中,但无法找到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. 但是当我打印元素时,它保存为“ null”,如预期的那样是定位器ID的某个地址。 Further by using the address of id I would had used with sendkeys to enter the email address in the text box. 此外,通过使用id的地址,我将可以使用sendkeys在文本框中输入电子邮件地址。

But the following error was displayed: 但是显示了以下错误:

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

Login class-where the locator issue exists: at - Login1.userName(driver).sendKeys(sUsername); 登录类-存在定位器问题的位置: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); 这是SigniN类,其中我正在获取Java空指针异常-问题存在: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);
}

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

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? 只是在这里吐痰,但除了上面提到的复制/粘贴问题外。.我看不到要在哪里为正在创建的驱动程序实例加载gmail页面的“获取”操作? 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: 另外,在执行findElement之前,对“ Email”字段进行明确的等待可能是一个好主意,因为页面可能仍在呈现:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium WebDriver-在尝试使用“ getAttribute”获取属性值时获取“ java.lang.NullPointerException” - Selenium WebDriver - Getting “java.lang.NullPointerException” while trying to get attribute value using “getAttribute” java.lang.NullPointerException 在点击事件中 Selenium WebDriver com Java - java.lang.NullPointerException in click event on Selenium WebDriver com Java java.lang.NullPointerException 使用 webdriver manager selenium java 时 - java.lang.NullPointerException when using webdriver manager selenium java 在 selenium Webdriver 中执行 TestNg.xml 文件时显示 java.lang.NullPointerException - java.lang.NullPointerException is displayed while executing TestNg.xml file in selenium Webdriver 为 selenium webdriver 类获取 java.lang.NullPointerException - Getting java.lang.NullPointerException for selenium webdriver class 尝试在webDriver中使用@FindBy时获取java.lang.NullPointerException - Getting java.lang.NullPointerException when trying to use @FindBy in webDriver java.lang.NullPointerException Webdriver故障 - java.lang.NullPointerException Webdriver Failure 尝试从数据库中获取数据时出现java.lang.NullPointerException - java.lang.NullPointerException while trying to fetch data from database 尝试通过ID获取实体时出现java.lang.NullPointerException - java.lang.NullPointerException while trying to fetch entity by id Webelement.click()在appium中给出java.lang.NullPointerException - Webelement.click() giving java.lang.NullPointerException in appium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM