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();