调用第二个方法时InputElement,报空指针java.lang.NullPointerException
来源:2-7 Input输入元素操作

慕后端5228983
2020-02-10
代码如下
package com.imooc.web;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Ten {
public WebDriver driver;
public void InitDriver(){
System.setProperty(“webdriver.chrome.driver”,“E:\java\chromedriver_win32\chromedriver.exe”);
WebDriver driver;
driver = new ChromeDriver();
driver.get(“http://www.imooc.com”);
driver.findElement(By.id(“js-signin-btn”)).click();
}
public void InputElement() {
WebElement element = driver.findElement(By.name("email"));
String UserInfo = element.getAttribute("placeholder");
element.sendKeys("18582682816");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(UserInfo);
element.clear();
String Mobile = element.getAttribute("value");
System.out.println(Mobile);
boolean info = element.isEnabled();
System.out.println(info);
}
public static void main(String[] args) {
Ten ten = new Ten();
ten.InitDriver();
ten.InputElement();
// TODO Auto-generated method stub
}
}
报错如下:
Starting ChromeDriver 2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1) on port 42069
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
二月 10, 2020 10:59:38 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: OSS
Exception in thread “main” java.lang.NullPointerException
at com.imooc.web.Ten.InputElement(Ten.java:21)
at com.imooc.web.Ten.main(Ten.java:44)
第21行:WebElement element = driver.findElement(By.name(“email”));
第44行: ten.InputElement();
麻烦老师抽空解答,蟹蟹
1回答
-
Exception in thread “main” java.lang.NullPointerException 说明你driver有吗?初始化了么/
122020-02-14
相似问题