在 selenium 中遇到错误 "Exception in thread main java.lang.nullpointerexception"

Facing an error "Exception in thread main java.lang.nullpointerexception" in selenium

我是 selenium 的新手。遇到上面提到的错误。 这是代码:-

package e3;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Login {
     public WebDriver drv;
    public WebDriver firefox= new FirefoxDriver();

public void new_account()
{

    firefox.findElement(By.cssSelector("a[title='Create New User']")).click();
    WebDriverWait wait = new WebDriverWait(drv,30);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("user-username"))).isDisplayed();
    firefox.findElement(By.id("user-username")).sendKeys("testone");

    firefox.findElement(By.id("user-password")).sendKeys("polaris");

    firefox.findElement(By.id("confirmPassword")).sendKeys("polaris");

    firefox.findElement(By.name("Account.name")).sendKeys("test");

    firefox.findElement(By.name("Account.surname")).sendKeys("one");

    firefox.findElement(By.name("saveform")).click();

}

public static void main (String args[])
{
Login login=new Login();    
login.open_url();
login.open_group();
//login.Addnew_group();
login.new_account();

}
}

在上面的代码中,我试图打开一个新帐户并保存它。在 Web 中,当它打开所需页面时,会出现正在加载标志,因此我插入了一个显式等待。现在它向我显示“空指针”错误。 请帮我解决这个问题。

谢谢,

您在代码中使用了一个名为 firefox 的驱动程序,但为了等待您添加了一个名为 drv 的驱动程序。此驱动程序未初始化导致错误。

您希望等待为您实际使用的驱动程序工作。我认为以下调整将使其适合您:

WebDriverWait wait = new WebDriverWait(firefox,30);