gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:38:27 25 4
gpt4 key购买 nike

编写了完整的代码来从 Excel 中获取数据并登录 Gmail,但在尝试这样做时,我的浏览器已打开,所需的页面也已打开,并且登录 ID 已从 Excel 中选取并存储在变量 sUsername 中,但无法找到 xpath as- element=driver.findElement(by.id("Email")); 但是当我打印元素时它保存“null”,正如预期的那样是某个地址定位器 ID。此外,通过使用 id 地址,我将与 sendkeys 一起使用,在文本框中输入电子邮件地址。

但显示以下错误:

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

登录类 - 存在定位器问题的位置: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;
}
}

这是 SigniN 类,其中 iam 遇到 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();
}
}

这是我实例化浏览器的地方--

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

最佳答案

最好的做法是在内部处理以及显式等待定位元素。如果有页面相关的 Activity ,那么还需要使用等待页面加载。

请按照以下代码操作对于内部等待

protected WebElement waitForPresent(final String locator) {
// timeout is your default wait timeout in long.
return waitForPresent(locator, timeout);
}

对于显式等待

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

关于java - Webdriver selenium,java.lang.NullPointerException,尝试查找 Webelement 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35041341/

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