This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
5年前关闭。
下面是例外情况,
您可以将其初始化为
这里的15是超时值(以秒为单位),您可以根据需要进行更改。您也可以使用documentation中提供的其他构造函数。
(12个答案)
5年前关闭。
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
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.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class EmployeeTest {
@FindBy(xpath="//input[@id='email']")
public static WebElement emailIDField;
@FindBy(xpath="//input[@id='password']")
public static WebElement passwordField;
@FindBy(xpath="//a[@id='submitButton']")
public static WebElement loginButton;
static String subscriptionsAndBillingTabXpath="//a[contains(text(),'Silling')]";
@FindBy(xpath="//a[contains(text(),'Silling')]")
public static WebElement subscriptionsAndBillingTab;
public static WebDriver driver;
public static WebDriverWait wait;
public static void main(String[] args) {
// TODO Auto-generated method stub
driver = new FirefoxDriver();
driver.get("http://url.domain.com");
wait.until(ExpectedConditions.visibilityOf(emailIDField));
emailIDField.click();
emailIDField.sendKeys("[email protected]");
wait.until(ExpectedConditions.visibilityOf(passwordField));
passwordField.click();
passwordField.sendKeys("xyz");
loginButton.click();
wait.until(ExpectedConditions.visibilityOf(subscriptionsAndBillingTab));
if(driver.findElements(By.xpath(subscriptionsAndBillingTabXpath)).size()!=0)
System.out.println("Login successful!");
else
System.out.println("Failed to login!");
driver.close();
}
}
下面是例外情况,
Exception in thread "main" java.lang.NullPointerException
at org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:227)
at org.openqa.selenium.support.ui.ExpectedConditions.access$1(ExpectedConditions.java:226)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:213)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:1)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)
at EmployeeTest.main(EmployeeTest.java:43)
最佳答案
您尚未初始化wait
,因此其为null并在下一行上抛出NPE
wait.until(ExpectedConditions.visibilityOf(emailIDField));
您可以将其初始化为
wait = new WebDriverWait(driver, 15);
这里的15是超时值(以秒为单位),您可以根据需要进行更改。您也可以使用documentation中提供的其他构造函数。
08-04 11:27