0

** Wenn ich diesen Code ausführen. Ich bekomme diesen Fehler Exception in thread "main" java.lang.NullPointerException. Kann jemand mir sagen, wie man es repariert?Selen-Webdriver mit JAVA

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

Antwort

0

Ich glaube nicht, dass es ein Element für diese Aussage gibt. So fügen Sie bitte Check für null vor dem Aufruf von 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

** i die same.Now iam getan haben bekommen „Keine solche Fenster Ausnahme“ ** –

+0

Dies ist ein häufiges Problem. Sie haben explizite Wartezeit oder implizite Wartezeit verwendet driver.manage() .timeouts(). ImplicitlyWait (30, TimeUnit.SECONDS) oder müssen WebDriverWait verwenden wait = new WebDriverWait (driver, 2); und warten –

0

HALLO bitte versuchen Sie es wie unten mit einer geringfügigen Änderung der Code begann zu arbeiten

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

// auch benutzen Sie bitte Treiberinstanz nicht, nachdem Sie verwendet haben driver.quit(); }

0

Endlich Schritte, die Sie unter Code verwenden:

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

Wenn Sie Treiber verwenden, beenden Sie(), alle Browser schließen und Session zerstören, so dass, wenn Sie driver.close add() es null erzeugen Zeigerausnahme Sie fügen keinen Selen-Webdriver-Code nach driver.quit() hinzu. Wenn Sie beide fügen Sie dann unten Sequenz hinzufügen in:

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