0

**当我运行此代码。我得到这个错误Exception in thread "main" java.lang.NullPointerException。任何人都可以告诉我如何解决它?与JAVA的硒webdriver

package Insights; 

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
public class Test_getinsights { 
    static WebDriver driver; 

    public static void main(String[] args) throws Exception { 
     System.setProperty("webdriver.chrome.driver", "C://selenium-2.53.0/chromedriver.exe"); 
     WebDriver driver = new ChromeDriver(); 
     driver.get("https://getinsights.co"); 
     driver.manage().window().maximize(); 

     //Login// 
     driver.findElement(By.xpath("html/body/div[1]/div/nav/div/div[2]/ul/li[7]/a")).click(); 

     //Login_page// 
     driver.findElement(By.xpath(".//*[@id='form']/div[1]/input")).sendKeys("[email protected]"); 
     driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/div[2]/input")).sendKeys("airloyal"); 
     driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/div[3]/div[2]/button")).click(); 

     //Create_survey// 
     Thread.sleep(10000); 
     driver.findElement(By.xpath(".//*[@id='rootId']/div[2]/div[1]/div[4]/div[2]/div/div/button")).click(); 

     //Create_Question// 
     driver.manage().timeouts().implicitlyWait(700, TimeUnit.SECONDS); 
     driver.findElement(By.xpath("html/body/div[1]/div/div/div[2]/div[1]/div/button[2]")).click(); 
     driver.findElement(By.xpath(".//*[@id='rootId']/div/div/div[2]/div[2]/div[1]/div[3]/div/div/div/button[2]")).click(); 
     System.out.println("driver=" + driver); 
     ****driver.findElement(By.xpath(".//*[@id='question 14998']/div/textarea")).sendKeys("Which is the best website to learn JAVA?");**** // Null pointer Exception// 
     driver.findElement(By.xpath(".//*[@id='rootId']/div/div/div[2]/div[2]/div[1]/div[5]/button")).click(); 

     //Post_Question 
     driver.findElement(By.cssSelector(".clear.openTextStyle.ng-pristine.ng-untouched.ng-invalid.ng-invalid-required")).sendKeys("Java"); 

     driver.findElement(By.cssSelector(".launch-btn.ng-binding")).click(); 

     driver.quit(); 
     driver.close(); 
    } 
} 

回答

0

我不认为这个陈述有任何元素。所以,请调用的SendKeys

WebElement element = driver.findElement(By.xpath(".//*[@id='question 14998']/div/textarea")); 
if (element != null){ 
    element.sendKeys("Which is the best website to learn JAVA?") 
}else 
//Do some thing 
} 
+0

**我也做了same.Now IAM得到“没有这样的窗口例外” ** –

+0

这是一个常见的问题。您有使用明确的等待或隐含的等待 driver.manage()。timeouts()。implicitlyWait(30,TimeUnit.SECONDS)或需要使用WebDriverWait wait = new WebDriverWait(driver,2);和wait.until –

0

HI请尝试像下面有轻微的改变之前增加检查空你的代码开始工作

public static void main(String[] args) throws Exception { 
      //System.setProperty("webdriver.chrome.driver", "C://selenium-2.53.0/chromedriver.exe"); 
      WebDriver driver = new FirefoxDriver(); 
      driver.manage().timeouts().implicitlyWait(700, TimeUnit.SECONDS); 
      driver.get("https://getinsights.co"); 
      driver.manage().window().maximize(); 

     //Login// 
     driver.findElement(By.xpath("html/body/div[1]/div/nav/div/div[2]/ul/li[7]/a")).click(); 

     //Login_page// 
     driver.findElement(By.xpath("//*[@id='form']/div[1]/input")).sendKeys("[email protected]"); 
     driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/div[2]/input")).sendKeys("airloyal"); 
     driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/div[3]/div[2]/button")).click(); 

     //Create_survey// 
     Thread.sleep(10000); 
     driver.findElement(By.xpath("//*[@id='rootId']/div[2]/div[1]/div[4]/div[2]/div/div/button")).click(); 

     //Create_Question// 

     driver.findElement(By.xpath("html/body/div[1]/div/div/div[2]/div[1]/div/button[2]")).click(); 
     driver.findElement(By.xpath("//*[@id='rootId']/div/div/div[2]/div[2]/div[1]/div[3]/div/div/div/button[2]")).click(); 
     System.out.println("driver=" + driver); 
     driver.findElement(By.xpath("//*[@placeholder='Type your Question']")).sendKeys("Which is the best website to learn JAVA?"); 
// Null pointer Exception //here i simply changed the xpath and its working 
     driver.findElement(By.xpath("//*[@id='rootId']/div/div/div[2]/div[2]/div[1]/div[5]/button")).click(); 

     //Post_Question 
     driver.findElement(By.cssSelector(".clear.openTextStyle.ng-pristine.ng-untouched.ng-invalid.ng-invalid-required")).sendKeys("Java"); 

     driver.findElement(By.cssSelector(".launch-btn.ng-binding")).click(); 

     driver.quit(); 

//也请不要使用驱动程序实例,您使用后driver.quit(); }

0

在您使用下面的代码最后一步:

driver.quit(); 
    driver.close(); 

当您使用的驱动程序,退出()它关闭所有浏览器和销毁会话,所以当你添加driver.close()它产生空指针异常。你不会在driver.quit()之后添加任何selenium webdriver代码。 如果要添加两种然后添加以下顺序:

driver.close(); 
    driver.quit();