我正在编写一个Java代码,它将在Chrome或Firefox中运行简单的自动化场景 - 具体取决于用户的输入。它开始运行(打开浏览器(,但随后抛出java.lang.NullPointerException。我以为我分配给驱动程序变量的 null 稍后会被覆盖,但事实并非如此。如何解决这个问题?谢谢!
package com.selenium;
import java.util.Scanner;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Main {
public static void main(String[] args) throws InterruptedException {
// environment variable
System.setProperty("webdriver.chrome.driver", "C:\Automation\libs\Drivers\chromedriver.exe");
//WebDriver chromeDriver = new ChromeDriver();
System.setProperty("webdriver.gecko.driver", "C:\Automation\libs\Drivers\geckodriver.exe");
WebDriver driver = null;
Scanner scanner = new Scanner(System.in);
int option = scanner.nextInt();
System.out.println("Please enter 1 for Chrome or 2 for Firefox " + option);
if (option == 1)
{
WebDriver driver1= new FirefoxDriver();
}
else if
(option == 2)
{
WebDriver driver2 = new ChromeDriver();
}
else
System.out.println("Please enter a correct number " + option);
String baseURL = "https://login.salesforce.com/?locale=eu";
driver.get(baseURL);
WebElement userName = driver.findElement(By.id("username"));
userName.sendKeys("Yan");
WebElement password = driver.findElement(By.id("password"));
password.sendKeys("123456");
WebElement rememberCheckbox = driver.findElement(By.id("rememberUn"));
rememberCheckbox.click();
WebElement bLogin = driver.findElement(By.id("Login"));
bLogin.click();
}
}
永远不会分配驱动程序,您只需创建新的驱动程序。改变:
if (option == 1)
{
WebDriver driver1= new FirefoxDriver();
}
else if
(option == 2)
{
WebDriver driver2 = new ChromeDriver();
}
自:
if (option == 1)
{
driver = new FirefoxDriver();
}
else if
(option == 2)
{
driver = new ChromeDriver();
}
我猜driver.get(baseURL);
投掷。在条件语句的主体中,您正在创建新变量(驱动程序 1、驱动程序 2(,并且永远不要使用它们。请为先前声明的driver
变量赋值。
if (option == 1)
{
WebDriver driver1= new FirefoxDriver();
}
会成为
if (option == 1)
{
driver = new FirefoxDriver();
}
和
else if (option == 2)
{
WebDriver driver2 = new ChromeDriver();
}
会成为
else if (option == 2)
{
driver = new ChromeDriver();
}
查看行driver.get(baseURL)
您在此对象上调用方法,但此时此对象仍处于null
状态。
这就是发生NullPointerException的原因。
以下是您问题的答案:
您必须在代码中处理许多事实,如下所示:
-
保持
environment variable
的配置与System.setProperty
一样,特别是for()
循环。 -
打印文本后保持行
scanner.nextInt()
Please enter 1 for Chrome or 2 for Firefox
。 -
使用完扫描程序实例后,请将其关闭以防止将来出现
Resource Leakage
。 -
在整个
Automation Framework
范围内,将WebDriver
实例的名称保留为driver
。 - 在您的代码中,您
WebDriver
实例声明为null
,并且您尝试启动driver1
或再次driver2
。因此,您面临java.lang.NullPointerException
-
以下是根据您的要求的示例代码块,它将使用Mozilla Firefox,浏览到url
https://login.salesforce.com/
,提供用户名和密码,最后单击Log In
按钮:package demo; import java.util.Scanner; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Q45482474 { public static void main(String[] args) { WebDriver driver = null; Scanner scanner = new Scanner(System.in); System.out.println("Please enter 1 for Chrome or 2 for Firefox : "); int option = scanner.nextInt(); scanner.close(); if (option == 1) { System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe"); driver= new FirefoxDriver(); } else if (option == 2) { System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe"); driver = new ChromeDriver(); } else System.out.println("Please enter a correct number."); String baseURL = "https://login.salesforce.com/?locale=eu"; driver.get(baseURL); WebElement userName = driver.findElement(By.id("username")); userName.sendKeys("Yan"); WebElement password = driver.findElement(By.id("password")); password.sendKeys("123456"); WebElement rememberCheckbox = driver.findElement(By.id("rememberUn")); rememberCheckbox.click(); WebElement bLogin = driver.findElement(By.id("Login")); bLogin.click(); } }
在 执行此程序时,您将在控制台上看到以下输出:
Please enter 1 for Chrome or 2 for Firefox : 1 1501764400779 geckodriver INFO geckodriver 0.18.0 1501764400792 geckodriver INFO Listening on 127.0.0.1:35604 1501764401293 geckodriver::marionette INFO Starting browser C:Program FilesMozilla Firefoxfirefox.exe with args ["-marionette"] 1501764421881 Marionette INFO Listening on port 48303 Aug 03, 2017 6:17:02 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C
让我知道这是否回答了您的问题。