获取内联代码的NullPointerException.我已经全局定义了WebDriver,调用浏览器方法工作正常,我在


public class Test{
static WebDriver driver;
public static void main(String[] arg) throws IOException, InterruptedException {
Test2();
}
public static void CallBrowserChrome() {
try {
System.setProperty("webdriver.chrome.driver", "G:\Chrome\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://google.com");

} catch (Exception e) {
e.printStackTrace();
}
}
public static void Test2() throws IOException, InterruptedException {
CallBrowserChrome();
FileInputStream fis = new FileInputStream("G:\Book1.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
int rows = sheet.getLastRowNum();
System.out.println(rows);
for (int i = 1; i <= rows; i++) {
String value = sheet.getRow(i).getCell(0).getStringCellValue();
System.out.println(value);
**driver.findElement(By.xpath("//*[@id='js-main-container']"))
.sendKeys(value);**
}
}
}

//错误是在这一行高亮显示(驱动程序在第二类);我试过在方法的主体内给出WebDriver,但这也不起作用

[1]: https://i.stack.imgur.com/0GzeA.png

这一行:WebDriver driver = new ChromeDriver();声明了CallBrowserChrome本地的驱动程序,并且忽略了您的其他声明。

把你的第一个方法改成:

public static void CallBrowserChrome() {
try {
System.setProperty("webdriver.chrome.driver", "G:\Chrome\chromedriver.exe");
driver = new ChromeDriver();// update this line
driver.get("https://google.com");
//...etc

最新更新