简体   繁体   English

使用Selenium时收到“线程“ main” java.lang.NullPointerException中的异常”

[英]Receiving “Exception in thread ”main“ java.lang.NullPointerException” when using Selenium

When using this class in Selenium WebDriver: 在Selenium WebDriver中使用此类时:

package Selenium3;

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Jobs {

static WebDriver driver;

public Jobs(WebDriver driver) {
    this.driver = driver;
}

public static void con() throws InterruptedException {
    List<WebElement> element = driver.findElements(By.cssSelector(".position_title.ng-binding"));
    for (int i = 0; i < element.size(); i++) {
        Thread.sleep(2000);
        String u = element.get(i).getText();
        if (u.contains("Java"));
        System.out.println(u);
    }
  }
}

The driver is always null. 驱动程序始终为null。 In the end I want to call this method from my Main class. 最后,我想从我的Main类中调用此方法。 What am I doing wrong? 我究竟做错了什么?

You have an static method (that not requires class instantiation)... so when you call the method without instantiating the class you wont initialize your driver: 您有一个static方法(不需要类实例化)...因此,当您在不实例化类的情况下调用该方法时,您将不会初始化驱动程序:

You have various solutions: 您有各种解决方案:

  • Make method not static (you will force initialization of driver with constructor, but you can reuse the instantiated class various times) 使方法不是static (您将使用构造函数强制执行驱动程序的初始化,但可以多次重用实例化的类)

     public class Jobs { private WebDriver driver; public Jobs(WebDriver driver) { this.driver = driver; } public void con() throws InterruptedException { List<WebElement> element = driver.findElements(By.cssSelector(".position_title.ng-binding")); for (int i = 0; i < element.size(); i++) { Thread.sleep(2000); String u = element.get(i).getText(); if (u.contains("Java")); System.out.println(u); } } 
  • Keep method static adding driver as a parameter (so you will have your driver prior to call method and you won't need to instantiate the class) 保持将方法static添加驱动程序作为参数 (因此您将在调用方法之前拥有驱动程序,并且无需实例化该类)

     public class Jobs { public static void con(WebDriver driver) throws InterruptedException { List<WebElement> element = driver.findElements(By.cssSelector(".position_title.ng-binding")); for (int i = 0; i < element.size(); i++) { Thread.sleep(2000); String u = element.get(i).getText(); if (u.contains("Java")); System.out.println(u); } } 
  • Use a static block to initialize the driver prior the method static call (faster to call, but problem is you cannot chuoose which driver implementation you will use) 使用static在方法静态调用之前初始化驱动程序 调用速度更快,但是问题是您无法选择将使用的驱动程序实现)

     public class Jobs { static WebDriver driver; static { this.driver = new FirefoxDriver(); // just an example } public static void con() throws InterruptedException { List<WebElement> element = driver.findElements(By.cssSelector(".position_title.ng-binding")); for (int i = 0; i < element.size(); i++) { Thread.sleep(2000); String u = element.get(i).getText(); if (u.contains("Java")); System.out.println(u); } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用 Java 在 Selenium 中运行测试用例时,线程“主”java.lang.NullPointerException 出现异常 - Exception in thread "main" java.lang.NullPointerException when trying to run a test case in Selenium with Java 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException “线程“ main”中的异常java.lang.NullPointerException” - “Exception in thread ”main“ java.lang.NullPointerException” 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException , 线程“main”中的异常 java.lang.NullPointerException - ,Exception in thread "main" java.lang.NullPointerException 线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException 线程“main”中的异常 java.lang.NullPointerException 5 - Exception in thread "main" java.lang.NullPointerException 5 添加到HashMapList时,“主线程中的异常” java.lang.NullPointerException - “Exception in thread ”main" java.lang.NullPointerException when adding to a HashMapList 使用Jaxb unmarshal / marshal时,线程“ main”中的异常java.lang.NullPointerException - Exception in thread “main” java.lang.NullPointerException when using Jaxb unmarshal/marshal Java:线程“main”java.lang.NullPointerException中的异常 - Java: Exception in thread “main” java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM