**當我運行此代碼。我得到這個錯誤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();
}
}
**我也做了same.Now IAM得到「沒有這樣的窗口例外」 ** –
這是一個常見的問題。您有使用明確的等待或隱含的等待 driver.manage()。timeouts()。implicitlyWait(30,TimeUnit.SECONDS)或需要使用WebDriverWait wait = new WebDriverWait(driver,2);和wait.until –