gpt4 book ai didi

java - 获取以下代码片段的 java NPE。不确定出了什么问题

转载 作者:行者123 更新时间:2023-12-01 12:03:09 25 4
gpt4 key购买 nike

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("abc@abc.com");

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 中给出的其他构造函数。 。

关于java - 获取以下代码片段的 java NPE。不确定出了什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27855786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com