如何使用SeleniumWebDriver解决java中的空指针异常
我正在SeleniumWebDriver中运行下面显示的代码。我正在创建类如何使用SeleniumWebDriver解决java中的空指针异常,java,selenium,selenium-webdriver,automated-tests,Java,Selenium,Selenium Webdriver,Automated Tests,我正在SeleniumWebDriver中运行下面显示的代码。我正在创建类SalesForceApplicationMethod()的m1对象,然后访问.validation()方法。代码在方法内部(.validation()),但它给出了以下错误: 错误追溯: package com.syntel.pratice; import java.util.Scanner; import java.util.concurrent.TimeUnit; import org.openqa.selenium
SalesForceApplicationMethod()
的m1
对象,然后访问.validation()
方法。代码在方法内部(.validation()
),但它给出了以下错误:
错误追溯:
package com.syntel.pratice;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SalesForceApplicationMethod
{
WebDriver driver;
public void validation()
{
// App launcher clicking
WebDriverWait wait = new WebDriverWait(driver,25);
WebElement ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='label slds-truncate slds-text-link']")));
ele.click();
// New button
WebElement newBtn1 = driver.findElement(By.xpath("//div[contains(text(),'New')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", newBtn1);
// Account name - first account
driver.findElement(By.xpath("//input[@class='input uiInput uiInputText
uiInput--default uiInput--input']")).sendKeys("srieedherks");
// Click Save
driver.findElement(By.xpath("//button[@class='slds-button slds-button--neutral uiButton--default uiButton--brand uiButton forceActionButton']")).click();
// Clicking new contact button
WebDriverWait wait1 = new WebDriverWait(driver, 20);
WebElement newbt = driver.findElement(By.xpath("//div[contains(text(),'New
Contact')]"));
JavascriptExecutor exe = (JavascriptExecutor)driver;
exe.executeScript("arguments[0].click();", newbt);
WebElement ele1 = driver.findElement(By.linkText("--None--"));
ele1.sendKeys("Mr.");
System.out.println("Selecting ");
// Entering first name
driver.findElement(By.xpath("//input[@class='compoundBorderBottom form-
element__row input']")).sendKeys("srieedher");
// Entering last name
driver.findElement(By.xpath("//input[@class='compoundBLRadius
compoundBRRadius form-element__row input']")).sendKeys("santhakumar");
// Click Save
driver.findElement(By.xpath("//button[@class='slds-button slds-button--
brand cuf-publisherShareButton undefined uiButton']")).click();
// Logout view profile
driver.findElement(By.xpath("//img[@src='https://c.ap5.content.force.com/pro
filephoto/005/T/1']")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", newbt);
// Click log out
driver.findElement(By.xpath("//a[contains(@class, 'profile-link-label
logout uiOutputURL')]")).click();
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
ChromeOptions o = new ChromeOptions();
o.addArguments("disable-extensions");
o.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(o);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://login.salesforce.com/");
driver.findElement(By.id("username")).sendKeys("srieedher@force.com");
driver.findElement(By.id("password")).sendKeys("Sriee678$");
driver.findElement(By.id("Login")).click();
Scanner s = new Scanner(System.in);
System.out.println("Enter your otp:");
String i = s.next().toString();
System.out.println("the OTP is : "+i);
driver.findElement(By.id("smc")).sendKeys(i);
driver.findElement(By.id("save")).click();
driver.findElement(By.xpath("//div[@class='slds-icon-Waffle']")).click();
SalesForceApplicationMethod m1 = new SalesForceApplicationMethod();
m1.validation();
}
}
线程“main”java.lang.NullPointerException中的异常位于com.google.common.base.Premissions.checkNotNull(预编辑org.openqa.selenium.support.ui.FluentWait.(FluentWait。java:102),位于org.openqa.selenium.support.ui.WebDriverWait.(WebDrivejava:71),位于org.openqa.selenium.support.ui.WebDriverWait.(WebDrivejava:45),位于com.syntel.pratice.SalesForceApplicationMethod.validation(SalesForceApplicationMjava:27),位于com.syntel.pratice.SalesForceApplicationMethod.main(SalesFor应用方法。爪哇:88)
我的代码:
package com.syntel.pratice;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SalesForceApplicationMethod
{
WebDriver driver;
public void validation()
{
// App launcher clicking
WebDriverWait wait = new WebDriverWait(driver,25);
WebElement ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='label slds-truncate slds-text-link']")));
ele.click();
// New button
WebElement newBtn1 = driver.findElement(By.xpath("//div[contains(text(),'New')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", newBtn1);
// Account name - first account
driver.findElement(By.xpath("//input[@class='input uiInput uiInputText
uiInput--default uiInput--input']")).sendKeys("srieedherks");
// Click Save
driver.findElement(By.xpath("//button[@class='slds-button slds-button--neutral uiButton--default uiButton--brand uiButton forceActionButton']")).click();
// Clicking new contact button
WebDriverWait wait1 = new WebDriverWait(driver, 20);
WebElement newbt = driver.findElement(By.xpath("//div[contains(text(),'New
Contact')]"));
JavascriptExecutor exe = (JavascriptExecutor)driver;
exe.executeScript("arguments[0].click();", newbt);
WebElement ele1 = driver.findElement(By.linkText("--None--"));
ele1.sendKeys("Mr.");
System.out.println("Selecting ");
// Entering first name
driver.findElement(By.xpath("//input[@class='compoundBorderBottom form-
element__row input']")).sendKeys("srieedher");
// Entering last name
driver.findElement(By.xpath("//input[@class='compoundBLRadius
compoundBRRadius form-element__row input']")).sendKeys("santhakumar");
// Click Save
driver.findElement(By.xpath("//button[@class='slds-button slds-button--
brand cuf-publisherShareButton undefined uiButton']")).click();
// Logout view profile
driver.findElement(By.xpath("//img[@src='https://c.ap5.content.force.com/pro
filephoto/005/T/1']")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", newbt);
// Click log out
driver.findElement(By.xpath("//a[contains(@class, 'profile-link-label
logout uiOutputURL')]")).click();
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
ChromeOptions o = new ChromeOptions();
o.addArguments("disable-extensions");
o.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(o);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://login.salesforce.com/");
driver.findElement(By.id("username")).sendKeys("srieedher@force.com");
driver.findElement(By.id("password")).sendKeys("Sriee678$");
driver.findElement(By.id("Login")).click();
Scanner s = new Scanner(System.in);
System.out.println("Enter your otp:");
String i = s.next().toString();
System.out.println("the OTP is : "+i);
driver.findElement(By.id("smc")).sendKeys(i);
driver.findElement(By.id("save")).click();
driver.findElement(By.xpath("//div[@class='slds-icon-Waffle']")).click();
SalesForceApplicationMethod m1 = new SalesForceApplicationMethod();
m1.validation();
}
}
因为
SalesForceApplicationMethod
类中的驱动程序未初始化。在该类中添加以下构造函数
public SalesForceApplicationMethod(WebDriver ldriver)
{
this.driver=ldriver;
}
在主类中创建此类实例时,传递WebDriver
SalesForceApplicationMethod m1 = new SalesForceApplicationMethod(driver);
希望这对你有帮助。谢谢。您已经创建了一个全局变量 网络驱动程序; 然后在您编写的函数main()中
WebDriver driver = new ChromeDriver(o);
它将是main()函数块的局部变量。可以做的一件简单的事情是初始化全局变量,而不是创建一个同名的新局部变量。为此,只需将main()块中的上述代码行替换为
driver = new ChromeDriver(o);
这将初始化在类开头声明的全局变量。您能添加完整的错误消息吗?就像@Asew指出的,没有完整的错误跟踪,调试您的问题非常困难<代码>代码进入方法内部(哪个方法?
),但它在方法的第一行给出了错误作为空指针异常
(哪个是第一行?)没有给我们任何信息@sriedhersanthakumar您没有为m1
instance@Asew,错误是位于org.openqa.selenium.support.ui.FluentWait.(FluentWait.java:102)的com.google.common.base.premissions.checkNotNull(premissions.java:212)上的线程“main”java.lang.NullPointerException中的异常在org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:71)在org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:45)在com.syntel.pratice.SalesForceApplicationMethod.validation(SalesForceApplicationMethod.java:27)在com.syntel.pratice.SalesForceApplicationMethod.main(SalesForceApplicationMethod.java:88)上请帮帮我。@iamdanchhiv,在org.openqa.selenium.support.ui.FluentWait.(FluentWait.java:102)org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:71)的com.google.common.base.premissions.checkNotNull(premissions.java:212)上的线程“main”java.lang.NullPointerException中的错误是异常org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:45)com.syntel.pratice.SalesForceApplicationMethod.validation(SalesForceApplicationMethod.java:27)com.syntel.pratice.SalesForceApplicationMethod.main(SalesForceApplicationMethod.java:88)非常感谢。代码运行良好。我想要退出的代码。由于实际问题已经解决,您可以将其作为单独的问题发布吗?我将作为单独的问题发布。问题只需在90分钟内提出。我会在90分钟后提问。